next up previous contents
Next: Advantages of Using Exception Up: Exception Handling Previous: Exception Handling   Contents

Exception Hierarchy

Exception can be defined by the following sentence:

An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

Exception-handling capability makes it possible to monitor exceptional conditions within a program, and to transfer control to special exception-handling code designed by a programmer whenever an exceptional condition is detected.

First, we try to execute the statements contained within a block surrounded by braces.

Then, if an exceptional condition is detected within that block, a programmer's code or the runtime system itself throws an exception object of a specific type. In JAVA that exception is an object derived, either directly, or indirectly, from the class Throwable.

The Throwable class in JAVA has two subclasses:

Error
indicates that a non-recoverable error has occurred that should not be caught. Errors usually cause the JAVA interpreter to display a message and exit.
Exception
indicates an abnormal condition that must be properly handled to prevent program termination.

Of all possible exceptions that JAVA can throw automatically, there is a subset for which catching and processing is optional. The compiler allows you to ignore the exceptions of this subset. If an actually ignoring exception occurs, it will be caught by the runtime system, and the program will be terminated.

The remaining exceptions, that can automatically be thrown in Java, must be recognised by program's code in order to compile your program.

Recognised means the program's code catch and either process the exception object using another piece of the code, or your code can pass it up to the next level in the method-invocation hierarchy for handling there.

JAVA program can then optionally execute a block of code designated by finally which is normally used to perform some type of cleanup which is needed whether or not an exception occurs.

If a method passes an exception up to the next level in the invocation hierarchy, this must be declared along with the method signature using the throws keyword.

If your code catches and processes an exception, the processing code can be as elaborate, or as simple as you want to make it. The fact is, simply ignoring it after you catch it will satisfy the compiler. This may, or may not be a good idea, depending on the type of the exception.

All exception objects inherit the methods of the Throwable JAVA class.


next up previous contents
Next: Advantages of Using Exception Up: Exception Handling Previous: Exception Handling   Contents
Olexiy Ye. Tykhomyrov 2001-12-16