Title: Java Card Technology Ch06: Exception and Exception Handling
1Java Card TechnologyCh06 Exception and
Exception Handling
- Instructors
- Fu-Chiung Cheng
- (???)
- Associate Professor
- Computer Science Engineering
- Tatung University
2Outline
- Exceptions in the java.lang Package
- Java Card Exceptions
- Java Card Exception Reason Code
- Throwing an Exception in the Java Card Platform
- ISOException
3Java Card Exceptions and Exception Handling
- 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
- They provide an elegant way of handling errors in
a platform.
4Java 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. - 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.
5Exceptions in the java.lang
- 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.
6Exceptions in the java.lang (cont.)
- The class Throwable defines common ancestor for
all the exception classes. - Applets can throw and catch only objects that
derive from the Throwable class.
7Exceptions 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 RuntimeException derives from the
Exception class, and it is the root class for all
unchecked exceptions.
8Exceptions in the java.lang (cont.)
9Java 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.
10Java Card Exceptions (cont.)
- 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.
11Java 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 typically indicate
unexpected runtime problemsprogramming error or
erroneous APDU processing state.
12Java Card Exceptions (cont.)
13Java Card Exceptions (cont.)
- 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.
14Java Card Exception Reason Code
- 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.
15Java Card Exception Reason Code (cont.)
- 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 - GetReason and SetReason
16Throwing an Exception in the Java Card Platform
- An applet creates an instance of an exception
class, the code is written as follows - throw new MyException(a specific error
message)
17Throwing an Exception in the Java Card Platform
(cont.)
- 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.
18Throwing 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. - 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
19Throwing an Exception in the Java Card Platform
(cont.)
- CardException and CardRuntimeExcep-tion provide a
static method throwIt for applets to reuse the
exception instance - public static void throwIt (short reason)
20Throwing 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. - ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWE
D)
21Throwing 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. - The applet reuses the instance whenever it needs
to throw that exception
22ISOException
- ISOException is a special unchecked exception in
the Java Card APIs. - ISOException encapsulates an ISO 7816 response
status word (SW) in its reason code.
23ISOException (cont.)
- The JCRE eventually catches an ISOException and
returns the reason code it contains as an ISO
status word to a host application. - Thats why the exception class carries ISO in its
name.
24ISOException (cont.)
- 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