2. The throwable object
The code that "handles" an exception may be far removed from the place where the exception occurred. A language must have a mechanism for identifying the exception. It also needs a mechanism for "throwing" the exception to a place where it will be caught and "handled". Java does this using the concept of a throwable object.
A throwable object is an instance of class
Throwable (or one of its
subclasses). Exception and Error are two subclasses of
class
From now on, we use the term exception for an object that has been thrown.
Click the icon to the left to learn in a 3.2 minute video what a throwable object is in Java. Java uses the concept of a class and its subclasses in many different ways! Here is a pdf file of the script (modified).
Exceptions in Java
Java has over 125 predefined
- ArithmeticException. Sometimes division by 0.
- ArrayIndexOutOfBoundsException. In something like b[i], i was not a legal value.
- ArrayStoreException. An attempt to assign a value of the wrong type to an array variable.
- NullPointerException. The equivalent of null.var or null.m(...) was attempted.
Errors in Java
An instance of class Error is a throwable object that a program is not expected to handle; instead, it should cause immediate termination of the program. A program CAN handle it, but it is advisable not to. The reason is that the error that caused the object to be thrown was so severe that one doesn't know what further execution of the program may do.
Java has about 25 predefined
exception hierarchy
Here's the basic exception hierarchy, with subclassing shown by indentation.
Throwable
Error (don't try to catch these)
OutOfMemoryError
IllegalAccessError
StackOverflowError
Exception
IOException
RuntimeException
See the four listed above
©This material is from the CD ProgramLive by David Gries and Paul Gries