Lesson 5: Exceptions

Overview

The exception mechanism consists of the try-catch block, the throw statement, and the throws clause of method declarations.

An exception is an object of class java.lang.Throwable, however you generally extend only Throwable's subclass java.lang.Exception. Either you or the system can create and throw an exception. Throwing an exception is partially like a return, partially like a control-C. The method returns to the caller immediately at the point of throw. If however the caller does not catch the thrown exception, the caller will also immediately return to its caller.

You can return automatically through several layers of method calls in this manner, until finally a method elects to catch the thrown exception.

Examples:

  1. ExEx1.java
  2. ExEx2.java
  3. ExEx3.java
  4. ExEx4.java
  5. ExEx5.java

The details

Java will not let a method throw an exception unexpectedly. At compile-time, the compiler must know what exceptions are possible. If a method throws an exception, it must use the throws clause in its declaration to notify the compiler of this. This includes methods which do not directly throw exceptions, but call methods which themselves throw exceptions. The compiler knows whether a method calls other methods which throw exceptions, because every method which throws an exception must have a throws clause in its declaration stating the type or range of types of exceptions that it might throw.

If a method class another method which throws an exception, the method might catch and handle the exception, rather than passing it to its own caller. To do this, any method capable of throwing an exception is placed in a try-catch block. If a method catches an exception, that exception is handled, and need not be declared in the throws clause. It will not be thrown to the caller. Instead, program flow will leave the try-catch block, enter the corresponding try block, and then, if there are no returns in the catch block, continue with the rest of the method after the try-catch block.

Catch blocks are arranged in a particular sequence at the end of the try-catch statement. There is also a finally clause, which is optional and we will not here discuss. The syntax of the try-catch block is strange, and specially rules govern it. Using the widening/narrowing type calculations, the first catch clause, proceeding down the page, which matches the type of the exception is selected and the other ignored. So the narrowest exception types must be placed before the wider ones if they are ever to be selected. To make use of this feature, you must subtype exception yourself to create more specific exceptions which your catch clauses will handle, ignoring more general exceptions.

All errors in Java are handled by exceptions. So as not to clutter the text with throws clauses, two classes of exceptions need not be declared: java.lang.RuntimeException, which is a direct subclass of java.lang.Exception; and java.lang.Error, which is a direct subclass of java.lang.Throwable. These are used by the runtime system for exceptions and errors such as IndexOutOfBoundsException and OutOfMemoryError.