Interface & Abstract Class
Download
Report
Transcript Interface & Abstract Class
Exception Handling
Background
The fact that software modules should be
robust enough to work under every
situation.
The exception handling mechanism is key
to achieving this goal.
Exception Terminology
Using the exception handling mechanism
in Java involves:
◦ identifying exception conditions relevant to
the application;
◦ locating exception handlers to respond to
potential conditions; and
◦ monitoring when such conditions occur.
Exception Terminology
As with all representations in Java,
exception conditions are denoted by
objects.
Similar with all objects, exceptions are
also defined by class constructs, but
inheriting attributes from the Exception
superclass.
While exception objects may be identified
by object tags, additional attributes may
be included for custom manipulation.
Exception Terminology
Exception handling is dynamically enabled
for statement blocks within a try block.
Within it, normal facilities and rules for
blocks apply but control-flow within may
be transferred to associated exception
handlers.
An appropriate statement block prefixed
by a catch-clause is then executed when
the associated exception condition
occurs.
Exception Terminology
The occurrence of an exception
condition is indicated by a throwstatement. It allows for an exception
object to be dynamically propagated to
the most recent exception handler. Flowcontrol does not return following a
throw-statement. Instead, execution
control proceeds at the statement
following the try-block that handles the
exception.
Constructs and Exception
Semantics in Java
We now consider the language primitives
for realizing the exception handling
framework in Java. As seen,
◦ exception objects are defined via class constructs
that inherit from the Exception class;
◦ exception handling is enabled within a try-block,
with handlers indicated by catch clauses; and
◦ an exception condition is identified by a throw
statement. (Some predefined exception
conditions are thrown implicitly by the Java
Virtual Ma-chine.)
Defining Exception Objects
The smallest exception object in Java
merely extends from the Exception
superclass,as outlined in the class
definition for TransmissionError below:
Defining Exception Handlers
Exception handlers are introduced by the
catch-clause within a try-block prefix, of
which the following code fragment is
representative.
Raising Exceptions
An exception condition is ultimately
represented by an exception object
derived from the predefined Exception
class.
A condition is made known by throwing
an appropriate object via a throw
statement, to be subsequently caught by
an associated handler.