Content received from:

Exceptions -III: What's an exception and why do I care?
Thursday, February 28, 2008 (21:16:41)

Posted by jalex

Java's catch or specify requirement

Part I was published here, Part II was published here.

As stated previously, Java requires that a method either catch or specify all checked exceptions that can be thrown within the scope of the method. This requirement has several components that need further description: "catch", "specify," "checked exceptions," and "exceptions that can be thrown within the scope of the method."

Catch: a method can catch an exception by providing an exception handler for that type of exception.

Specify: if a method chooses not to catch an exception, the method must specify that it can throw that exception. Why did the Java designers make this requirement? Because any exception that can be thrown by a method is really part of the method's public programming interface: callers of a method must know about the exceptions that a method can throw in order to intelligently and consciously decide what to do about those exceptions. Thus, in the method signature you specify the exceptions that the method can throw.

Checked exceptions: Java has different types of exceptions, including I/O Exceptions, runtime exceptions, and exceptions of your own creation, to name a few. Of interest to us in this discussion are runtime exceptions. Runtime exceptions are those exceptions that occur within the Java runtime system. This includes arithmetic exceptions (such as when dividing by zero), pointer exceptions (such as trying to access an object through a null reference), and indexing exceptions (such as attempting to access an array element through an index that is too large or too small).

Runtime exceptions can occur anywhere in a program and in a typical program can be very numerous. The cost of checking for runtime exceptions often exceeds the benefit of catching or specifying them. Thus the compiler does not require that you catch or specify runtime exceptions, although you can. Checked exceptions are exceptions that are not runtime exceptions and are checked by the compiler; the compiler checks that these exceptions are caught or specified.

Some consider this a loophole in Java's exception handling mechanism, and programmers are tempted to make all exceptions runtime exceptions. In general, this is not recommended.

Exceptions that can be thrown within the scope of the method: this statement may seem obvious at first: just look for the throw statement. However, this statement includes more than just the exceptions that can be thrown directly by the method: the key is in the phrase within the scope of. This phrase includes any exception that can be thrown while the flow of control remains within the method. This statement includes both:

  • Exceptions that are thrown directly by the method with Java's throw statement.
  • Exceptions that are thrown indirectly by the method through calls to other methods.

The ListOfNumbers example

The following example defines and implements a class named ListOfNumbers. The ListOfNumbers class calls two methods from classes in the Java packages that can throw exceptions.


// Note: This class won't compile by design!
// See or
// for a version of this
class that will compile.
import java.util.Vector;
public class ListOfNumbers {
    private Vector victor;
    private static final int size = 10;
    public ListOfNumbers () {
        victor = new Vector(size);
        for (int i = 0; i < size; i++)
        victor.addElement(new Integer(i));
    public void writeList() {

    PrintWriter out=
                  new PrintWriter(new FileWriter("OutFile.txt"));
    for (int i = 0; i < size; i++)
        out.println("Value at: " + i + " = " + victor.elementAt(i));

Upon construction, ListOfNumbers creates a Vector that contains ten Integer elements with sequential values 0 through 9. The ListOfNumbers class also defines a method named writeList that writes the list of numbers into a text file called OutFile.txt.

The writeList method calls two methods that can throw exceptions. First, the following line invokes the constructor for FileWriter, which throws an IOException if the file cannot be opened for any reason:

out = new PrintWriter(new FileWriter("OutFile.txt"));

Second, the Vector class's elementAt method throws an ArrayIndexOutOfBoundsException if you pass in an index whose value is too small (a negative number) or too large (larger than the number of elements currently contained by the Vector). Here's how ListOfNumbers invokes elementAt:

out.println("Value at: " + i + " = " +

If you try to compile the ListOfNumbers class, the compiler prints an error message about the exception thrown by the FileWriter constructor, but does not display an error message about the exception thrown by elementAt. This is because the exception thrown by the FileWriter constructor, IOException, is a checked exception and the exception thrown by the elementAt method, ArrayIndexOutOfBoundsException, is a runtime exception. Java requires that you catch or specify only checked exceptions.