Java Card Technology Ch06: Exception and Exception Handling
-
Upload
hamish-holden -
Category
Documents
-
view
70 -
download
1
description
Transcript of Java Card Technology Ch06: Exception and Exception Handling
Java Card TechnologyCh06: Exception and Exception Handling
Instructors: Instructors:
Fu-Chiung Cheng Fu-Chiung Cheng
((鄭福炯鄭福炯 ))
Associate Professor Associate Professor
Computer Science & EngineeringComputer Science & Engineering
Tatung UniversityTatung University
Outline
Exceptions in the java.lang PackageExceptions in the java.lang Package Java Card ExceptionsJava Card Exceptions Java Card Exception Reason CodeJava Card Exception Reason Code Throwing an Exception in the Java Card Throwing an Exception in the Java Card
PlatformPlatform ISOExceptionISOException
Java Card Exceptions and Exception Handling An An exception exception is an event that disrupts is an event that disrupts
the normal flow of instructions during the normal flow of instructions during the execution of a program.the execution of a program.
Exceptions are important in the Java Exceptions are important in the Java languagelanguage They provide an elegant way of They provide an elegant way of
handling errors in a platform.handling errors in a platform.
Java Card Exceptions and Exception Handling A Java Card applet can use keywords A Java Card applet can use keywords throwthrow, ,
trytry, , catchcatch, or , or finallyfinally, and they work the same , and they work the same as in the Java platform.as in the Java platform.
Although the Java Card platform has full Although the Java Card platform has full support for Java-style exceptions, there are support for Java-style exceptions, there are differences in usagedifferences in usage Due to the restrictive environment of smart Due to the restrictive environment of smart
card.card.
Exceptions in the java.lang
The Java Card platform does not support all The Java Card platform does not support all the exception typesthe exception types Because many of them are not applicable Because many of them are not applicable
in a smart card context.in a smart card context.
Threads are not supported in the Java Card Threads are not supported in the Java Card platform, and as a result, none of the thread-platform, and as a result, none of the thread-related exceptions are supported.related exceptions are supported.
Exceptions in the java.lang (cont.) The class The class Throwable Throwable defines common defines common
ancestor for all the exception classes.ancestor for all the exception classes.
Applets can throw and catch only Applets can throw and catch only objects that derive from the Throwable objects that derive from the Throwable class.class.
Exceptions in the java.lang (cont.) The class The class ExceptionException extends from the extends from the
Throwable calss. It is the root class in Throwable calss. It is the root class in the Java Card platform for all checked the Java Card platform for all checked exceptions.exceptions.
The class The class RuntimeExceptionRuntimeException derives derives from the Exception class, and it is the from the Exception class, and it is the root class for all unchecked exceptions.root class for all unchecked exceptions.
Exceptions in the java.lang (cont.)
ThrowableThrowable ExceptionException RuntimeExceptionRuntimeException
Arithematic-Arithematic-ExceptionException
ArrayIndexOutOfBArrayIndexOutOfBoundsExceptionoundsException
ArrayStoreExceptionArrayStoreException
ClassCast-ClassCast-ExceptionException
IndexOutOfBoundIndexOutOfBoundsExceptionsException
NullPointer-Exception NullPointer-Exception
Security-Security-ExceptionException
NegativeArraySizeNegativeArraySizeExceptionException
Java Card Exceptions
Checked exceptions Checked exceptions are subclasses of are subclasses of the Exception class and must either be the Exception class and must either be caught in the throwing method or be caught in the throwing method or be declared in a throws clause of the declared in a throws clause of the method header.method header.
Java Card Exceptions (cont.)
All checked exceptions must eventually All checked exceptions must eventually be caught by the appletbe caught by the applet Checked exceptions indicate a Checked exceptions indicate a
programming error in an applet and programming error in an applet and thus should be corrected by the thus should be corrected by the applet.applet.
Checked exceptions are an important Checked exceptions are an important part of the interface to a method.part of the interface to a method.
Java Card Exceptions (cont.)
Unchecked exceptions (runtime exceptions)Unchecked exceptions (runtime exceptions) are subclasses of the class Runtime-are subclasses of the class Runtime-Exception and need neither be caught in a Exception and need neither be caught in a program nor declared in a throws clause.program nor declared in a throws clause.
Unchecked exceptionsUnchecked exceptions typically indicate typically indicate unexpected runtime problems—programming unexpected runtime problems—programming error or erroneous APDU processing state.error or erroneous APDU processing state.
Java Card Exceptions (cont.)
Throwable
Exception
CardException
RuntimeException
CardRuntimeException
Checked exceptions inJava Card technology
Unchecked exceptions inJava Card technology
Java Card Exceptionhierarchy
Java Card Exceptions (cont.)
Why do we need the classes Why do we need the classes CardException and CardRuntime-CardException and CardRuntime-Exception?Exception? Because they enable a resource-Because they enable a resource-
saving mechanism so that an saving mechanism so that an exception object can be reused exception object can be reused multiple times.multiple times.
Java Card Exception Reason Code The Java exception classes supply a The Java exception classes supply a
“message” string that indicates a “message” string that indicates a specific error.specific error.
As an alternative way to attach extra As an alternative way to attach extra information to the exception, the Java information to the exception, the Java Card exception classes supply a Card exception classes supply a numerical reason codenumerical reason code..
Java Card Exception Reason Code (cont.) The reason code is used to describe The reason code is used to describe
optional details related to the throwing optional details related to the throwing of the exception.of the exception.
CardException and CardRuntimeExcep-CardException and CardRuntimeExcep-tion define two public access methodstion define two public access methods GetReason and SetReasonGetReason and SetReason
Throwing an Exception in the Java Card Platform An applet creates an instance of an An applet creates an instance of an
exception class, the code is written as exception class, the code is written as followsfollows
throw new MyException(“a specific error throw new MyException(“a specific error message”);message”);
Throwing an Exception in the Java Card Platform (cont.) Space economy is always a concern in Space economy is always a concern in
a smart card.a smart card.
If an applet creates an object every time If an applet creates an object every time an exception is thrown, the applet will an exception is thrown, the applet will over time accumulate many unused over time accumulate many unused exception instances in precious exception instances in precious EEPROM memory. EEPROM memory.
Throwing an Exception in the Java Card Platform (cont.)
To optimize memory usage, all exception objects To optimize memory usage, all exception objects should pre-created at initialization time and their should pre-created at initialization time and their references saved permanently.references saved permanently.
When an exception event occurs, rather than When an exception event occurs, rather than creatcreating a new exception object, an applet can do ing a new exception object, an applet can do the following:the following: 1. Retrieve and reuse the references for 1. Retrieve and reuse the references for the the
desired exception objectdesired exception object 2. Fill in the reason code in the object2. Fill in the reason code in the object 3. Throw the object3. Throw the object
Throwing an Exception in the Java Card Platform (cont.) CardException and CardRuntimeExcep-CardException and CardRuntimeExcep-
tion provide a static method tion provide a static method throwIt throwIt for for applets to reuse the exception instanceapplets to reuse the exception instance
public static void throwIt (short reason)public static void throwIt (short reason)
Throwing an Exception in the Java Card Platform (cont.) To reject an APDU command, an applet To reject an APDU command, an applet
can throw an ISOException and indicate can throw an ISOException and indicate the reason code as “command not the reason code as “command not allowed”.allowed”.
ISOException.throwIt(ISO7816.SW_COMMAISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED)ND_NOT_ALLOWED)
Throwing an Exception in the Java Card Platform (cont.) During initialization, the applet During initialization, the applet
instantiates such an exception object instantiates such an exception object and saves the reference in a persistent and saves the reference in a persistent field.field. The applet reuses the instance The applet reuses the instance
whenever it needs to throw that whenever it needs to throw that exceptionexception
ISOException
ISOException is a special unchecked ISOException is a special unchecked exception in the Java Card APIs.exception in the Java Card APIs.
ISOException encapsulates an ISO ISOException encapsulates an ISO 7816 response status word (SW) in its 7816 response status word (SW) in its reason code.reason code.
ISOException (cont.)
The JCRE eventually catches an The JCRE eventually catches an ISOException and returns the reason ISOException and returns the reason code it contains as an ISO status word code it contains as an ISO status word to a host application.to a host application. That’s why the exception class carries That’s why the exception class carries
ISO in its name.ISO in its name.
ISOException (cont.)
The Java Card platform provides an The Java Card platform provides an interface javacard.framework.ISO7816 interface javacard.framework.ISO7816 that defines the most commonly used that defines the most commonly used status word constantsstatus word constants related to ISO 7816-3 and ISO 7816-related to ISO 7816-3 and ISO 7816-
4 4