Download Lecture 9

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Class (computer programming) wikipedia , lookup

Object-oriented programming wikipedia , lookup

Name mangling wikipedia , lookup

Scala (programming language) wikipedia , lookup

Java syntax wikipedia , lookup

Structured programming wikipedia , lookup

C Sharp syntax wikipedia , lookup

C++ wikipedia , lookup

Java (programming language) wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Java performance wikipedia , lookup

Transcript
Java Card Technology
Ch06: Exception and
Exception Handling
Instructors:
Fu-Chiung Cheng
(鄭福炯)
Associate Professor
Computer Science & Engineering
Tatung University
Outline
Exceptions in the java.lang Package
 Java Card Exceptions
 Java Card Exception Reason Code
 Throwing an Exception in the Java Card
Platform
 ISOException

Java 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.
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.

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.
Exceptions 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 threadrelated exceptions are supported.
Exceptions 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.
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 RuntimeException derives
from the Exception class, and it is the
root class for all unchecked exceptions.
Exceptions in the java.lang
(cont.)
Throwable
Exception
RuntimeException
ArithematicException
ArrayIndexOutOfB
oundsException
ArrayStoreException
ClassCastException
IndexOutOfBound
sException
NullPointer-Exception
SecurityException
NegativeArraySize
Exception
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.
Java 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.
Java Card Exceptions (cont.)

Unchecked exceptions (runtime exceptions)
are subclasses of the class RuntimeException 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.
Java Card Exceptions (cont.)
Throwable
Java Card Exception
hierarchy
Exception
RuntimeException
CardException
CardRuntimeException
Checked exceptions in
Java Card technology
Unchecked exceptions in
Java Card technology
Java Card Exceptions (cont.)

Why do we need the classes
CardException and CardRuntimeException?
 Because they enable a resourcesaving mechanism so that an
exception object can be reused
multiple times.
Java 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.
Java Card Exception Reason
Code (cont.)

The reason code is used to describe
optional details related to the throwing
of the exception.

CardException and CardRuntimeException define two public access methods
 GetReason and SetReason
Throwing 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”);
Throwing 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.
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.

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
Throwing an Exception in the
Java Card Platform (cont.)

CardException and CardRuntimeException provide a static method throwIt for
applets to reuse the exception instance
public static void throwIt (short reason)
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”.
ISOException.throwIt(ISO7816.SW_COMMA
ND_NOT_ALLOWED)
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.
 The applet reuses the instance
whenever it needs to throw that
exception
ISOException

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 (cont.)

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.
ISOException (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 78164