Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Card Technology Ch06: Exception and Exception Handling Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Computer.

Similar presentations


Presentation on theme: "Java Card Technology Ch06: Exception and Exception Handling Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Computer."— Presentation transcript:

1 Java Card Technology Ch06: Exception and Exception Handling Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Computer Science & Engineering Tatung University

2 Outline Exceptions in the java.lang Package Exceptions in the java.lang Package Java Card Exceptions Java Card Exceptions Java Card Exception Reason Code Java Card Exception Reason Code Throwing an Exception in the Java Card Platform Throwing an Exception in the Java Card Platform ISOException ISOException

3 Java Card Exceptions and Exception Handling An exception is an event that disrupts the normal flow of instructions during the execution of a program. An exception is an event that disrupts the normal flow of instructions during the execution of a program. Exceptions are important in the Java language Exceptions are important in the Java language  They provide an elegant way of handling errors in a platform.

4 Java Card Exceptions and Exception Handling A Java Card applet can use keywords throw, try, catch, or finally, and they work the same as in the Java platform. A Java Card applet can use keywords throw, try, catch, or finally, and they work the same as in the Java platform. Although the Java Card platform has full support for Java-style exceptions, there are differences in usage Although the Java Card platform has full support for Java-style exceptions, there are differences in usage  Due to the restrictive environment of smart card.

5 Exceptions in the java.lang The Java Card platform does not support all the exception types The Java Card platform does not support all the exception types  Because many of them are not applicable in a smart card context. Threads are not supported in the Java Card platform, and as a result, none of the thread- related exceptions are supported. Threads are not supported in the Java Card platform, and as a result, none of the thread- related exceptions are supported.

6 Exceptions in the java.lang (cont.) The class Throwable defines common ancestor for all the exception classes. The class Throwable defines common ancestor for all the exception classes. Applets can throw and catch only objects that derive from the Throwable class. Applets can throw and catch only objects that derive from the Throwable class.

7 Exceptions in the java.lang (cont.) The class Exception extends from the Throwable calss. It is the root class in the Java Card platform for all checked exceptions. The class Exception extends from the Throwable calss. It is the root class in the Java Card platform for all checked exceptions. The class RuntimeException derives from the Exception class, and it is the root class for all unchecked exceptions. The class RuntimeException derives from the Exception class, and it is the root class for all unchecked exceptions.

8 Exceptions in the java.lang (cont.) ThrowableExceptionRuntimeException Arithematic- Exception ArrayIndexOutOfB oundsException ArrayStoreException ArrayStoreException ClassCast- Exception IndexOutOfBound sException NullPointer-Exception Security- Exception NegativeArraySize Exception

9 Java Card Exceptions Checked exceptions are subclasses of the Exception class and must either be caught in the throwing method or be declared in a throws clause of the method header. Checked exceptions are subclasses of the Exception class and must either be caught in the throwing method or be declared in a throws clause of the method header.

10 Java Card Exceptions (cont.) All checked exceptions must eventually be caught by the applet All checked exceptions must eventually be caught by the applet  Checked exceptions indicate a programming error in an applet and thus should be corrected by the applet.  Checked exceptions are an important part of the interface to a method.

11 Java Card Exceptions (cont.) Unchecked exceptions (runtime exceptions) are subclasses of the class Runtime- Exception and need neither be caught in a program nor declared in a throws clause. Unchecked exceptions (runtime exceptions) are subclasses of the class Runtime- Exception and need neither be caught in a program nor declared in a throws clause. Unchecked exceptions typically indicate unexpected runtime problems—programming error or erroneous APDU processing state. Unchecked exceptions typically indicate unexpected runtime problems—programming error or erroneous APDU processing state.

12 Java Card Exceptions (cont.)

13 Why do we need the classes CardException and CardRuntime- Exception? Why do we need the classes CardException and CardRuntime- Exception?  Because they enable a resource- saving mechanism so that an exception object can be reused multiple times.

14 Java Card Exception Reason Code The Java exception classes supply a “message” string that indicates a specific error. The Java exception classes supply a “message” string that indicates a specific error. As an alternative way to attach extra information to the exception, the Java Card exception classes supply a numerical reason code. As an alternative way to attach extra information to the exception, the Java Card exception classes supply a numerical reason code.

15 Java Card Exception Reason Code (cont.) The reason code is used to describe optional details related to the throwing of the exception. The reason code is used to describe optional details related to the throwing of the exception. CardException and CardRuntimeExcep- tion define two public access methods CardException and CardRuntimeExcep- tion define two public access methods  GetReason and SetReason

16 Throwing an Exception in the Java Card Platform An applet creates an instance of an exception class, the code is written as follows An applet creates an instance of an exception class, the code is written as follows throw new MyException(“a specific error message”);

17 Throwing an Exception in the Java Card Platform (cont.) Space economy is always a concern in a smart card. Space economy is always a concern in a smart card. If an applet creates an object every time an exception is thrown, the applet will over time accumulate many unused exception instances in precious EEPROM memory. If an applet creates an object every time an exception is thrown, the applet will over time accumulate many unused exception instances in precious EEPROM memory.

18 Throwing an Exception in the Java Card Platform (cont.) To optimize memory usage, all exception objects should pre-created at initialization time and their references saved permanently. To optimize memory usage, all exception objects should pre-created at initialization time and their references saved permanently. When an exception event occurs, rather than creating a new exception object, an applet can do the following: When an exception event occurs, rather than creating a new exception object, an applet can do the following:  1. Retrieve and reuse the references for the desired exception object  2. Fill in the reason code in the object  3. Throw the object

19 Throwing an Exception in the Java Card Platform (cont.) CardException and CardRuntimeExcep- tion provide a static method throwIt for applets to reuse the exception instance CardException and CardRuntimeExcep- tion provide a static method throwIt for applets to reuse the exception instance public static void throwIt (short reason)

20 Throwing an Exception in the Java Card Platform (cont.) To reject an APDU command, an applet can throw an ISOException and indicate the reason code as “command not allowed”. To reject an APDU command, an applet can throw an ISOException and indicate the reason code as “command not allowed”. ISOException.throwIt(ISO7816.SW_COMMA ND_NOT_ALLOWED)

21 Throwing an Exception in the Java Card Platform (cont.) During initialization, the applet instantiates such an exception object and saves the reference in a persistent field. During initialization, the applet instantiates such an exception object and saves the reference in a persistent field.  The applet reuses the instance whenever it needs to throw that exception

22 ISOException ISOException is a special unchecked exception in the Java Card APIs. ISOException is a special unchecked exception in the Java Card APIs. ISOException encapsulates an ISO 7816 response status word (SW) in its reason code. ISOException encapsulates an ISO 7816 response status word (SW) in its reason code.

23 ISOException (cont.) The JCRE eventually catches an ISOException and returns the reason code it contains as an ISO status word to a host application. The JCRE eventually catches an ISOException and returns the reason code it contains as an ISO status word to a host application.  That’s why the exception class carries ISO in its name.

24 ISOException (cont.) The Java Card platform provides an interface javacard.framework.ISO7816 that defines the most commonly used status word constants The Java Card platform provides an interface javacard.framework.ISO7816 that defines the most commonly used status word constants  related to ISO 7816-3 and ISO 7816- 4


Download ppt "Java Card Technology Ch06: Exception and Exception Handling Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Computer."

Similar presentations


Ads by Google