Java Card Technology for Smart Card's architecture and programmer's Guide (zhiqun Chen)

Source: Internet
Author: User

Chapter 2 Java card exception and Exception Handling

 

One exception isProgramDuring the execution of the command. In Java, exceptions are important.
Because they provide an excellent way to handle program errors.
 
The Java card platform supports all exceptional Java programming structures. Java card applet can use the keyword throw, try,
Catch, or finally, and they run as they are on the Java platform.
 
When an internal running problem is detected, the JRE or Java card Virtual Machine throws an exception, or the applets throws the exception through a program.
Although the Java card platform supports all Java-style exceptions, its usage is different because of limited smart card usage.
. This chapter introduces some exceptions in the Java card platform and discusses how the applet throws and handles exceptions.
 
6.1. Exceptions in the Java. lang Package
Generally, the Java card platform does not support all the exception types visible in the Java technology core package, because many of them are
It cannot be used in the card environment. For example, threads are not supported in the Java card platform, and therefore thread-related exceptions are not supported.
 
However, it is true that the java. lang package on the Java card supports some exception classes in the Java version of the package. Among all supported exception classes,
Only the equals method inherited from the root class object and a construction method without parameters are provided. Table 6.1 lists
All exception classes in the Java. lang package.

The class throwable defines the common ancestor of all the exception classes on the Java platform. This class also ensures that Java card exceptions and their Java
The corresponding platform has the same semantics. For example, applets can only throw and capture objects created from the throwable class.
 
Class exception is extended from the throwable class. As on the Java platform, it is applicable to all
The root class of the exception. Runtimeexception class is derived from the exception class, which is all non-detection examples on the Java platform.
. The concept of detection and Non-Detection exceptions has been defined in the Java language specification. In the next section, we will also describe their settings.
.
 
All other classes in table 6.1 are non-detection exceptions. The exception classes in the Java. lang package provide the basic language for the Java exception architecture.
Yes. When an error occurs due to a Java language violation, these exceptions are thrown by the Java card virtual machine.
 
6.2.java card exceptions
The Java card platform provides a class inheritance hierarchy for detecting exceptions and Non-Detection exceptions, as shown in Figure 6.1.


 
The exception to be detected (checked exception) is a subclass of the exception class and must be captured in the throwing method,
Or declare it in the throws clause of the method header. This requirement is mandatory for Java compiling programs. All Java card exceptions to be detected
Is extended from the cardexception class, and the latter is derived from the class exception.

 
For two reasons, all exceptions to be detected must be captured by the applet. First, the exception to be detected indicates
This applet must correct a programming error. Second, exceptions to be detected are an important part of a method interface. This
That is why the applet API method should specify the exceptions to be checked in the throws clause (leave them to the applet for processing ). So if
When the applet does not capture exceptions to be detected, the Java Compilation Program sends an error indication.

 

Unchecked exception is a subclass of the runtimeexception class.
It does not need to be captured in the program or declared in theows clause. Exceptions that are not checked usually indicate unpredictable runtime problems and issues
Process error, or The APDU processing status is incorrect. Such exceptions are captured by the outermost layer of JRE. All non-
All exceptions should be extended from the cardruntimeexception class, which is extended from the runtimeexception class.
 
Why do we need cardexception and cardruntimeexception classes? Because they can make a machine that saves resources
It is possible that an exception object can be reused multiple times, as described in sections 6.2.1 and 6.2.2.

6.2.1.java card exception cause code

Java exception classes provide "message" strings indicating specific errors. The string class is not supported on the Java console, so no
Provides message strings. As another method to add some additional information to the exception, the Java card exception class adopts a digital
Cause code. The cause code is used to describe the optional details related to the exception throw. The type of the cause code is short.
 
The cause code is defined as a field in the cardexception class and cardruntimeexception class, and therefore inherited by their subclasses
. In addition, both Classes define two public accessor methods (getreason and setreason) to obtain and set the original
Cause code.
 
6.2.2. Exceptions are thrown on the Java console.
 
To throw a Java System exception, the applet needs to create an instance of an exception class,CodeWrite as follows:


Throw new myexception ("a specific error message ");

Of course, whenever an exception is thrown in the Java platform, you may need to create a new exception object. However, the bucket
The economic use of smart cards is always a matter of concern. If an exception is thrown, the applet creates an object,
With the passage of time, this applet will accumulate many unnecessary exception instances in the precious EEPROM Memory. To
Optimized memory usage. All exceptional objects should be created during initialization and their references should be permanently saved. When I
When an exception event occurs, you do not need to create a new exception object. The applet can be as follows:

1. Request and reuse references to the expected exceptions
2. Enter the Reason code in the object.
3. Throw this object

To support reusable exception objects, JRE has created an instance for each exception type in the Java card APIs. Class
Cardexception, cardruntimeexception, and each of their subclasses provide
The static method throwit:

Public static void throwit (short reason)

Every time the method throwit is called, it throws an exception instance that has been created by JRE. The applet is directed to the throwit party
Method to Identify a cause code. For example, to reject an APDU command, the applet can throw an isoexception and
Because the code is "command not allowed ":

Isoexception. throwit (iso7816.sw _ command_not_allowed );

An applet can also create its own exception object. During initialization, the applet instantiates such an exception object,
And save the reference in a permanent domain. In the future, this instance will be reused as long as it needs to throw this exception.

6.2.3.isoexception.

Isoexception is a special non-detection exception in the Java card platform. It occurs during running and indicates a warning or
An error in the card. Isoexception encapsulates an iso7816 status word in the cause code.

Isoexception allows the applet to efficiently handle errors. When a command is successfully processed, the method returns normally.
However, if an error occurs, this method simply throws an isoexception with an appropriate state word.

Generally, the applet does not handle the exception of isoexception. JRE finally captures the isoexception and returns a packet
The cause code, which is used as the ISO status word returned to the Host application. This causes the exception class to contain ISO in its name.

The ISO status word is part of the APDU protocol. It is used as a smart card application to return an APDU life to the Host application.
Processing status. The Java card platform provides an interface javacard. Framework. iso7816, which defines the most common ISO
The status word constants of 7816-3 and ISO 7816-4. An applet can define its own state word and use isoexception to put it
To the Host application.

6.2.4.userexception.
When an applet encounters a program error that needs to be corrected by the applet, it throws a userexception. No
Like isoexception, The userexception exception is the exception to be detected derived from cardexception. Therefore, the applet must
Processing. If the applet needs to create another exception type, it will create a class derived from userexception.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.