Exceptions Mini-Lecture – Part 1
October 22nd, 2008
Simply put, an exception indicates something went wrong. For instance, attempting to access an array with an index that does not fall within the array boundaries will cause an exception. Another example is attempting to convert a non-numeric string to an integer (e.g. Integer.parseInt("foo")).
When a method needs to communicate to its caller that an error has occurred, it is called “throwing” the exception. There are two related reserved words in Java for this purpose, throw and throws.
throw
Like (almost) everything in Java, an exception is an object. If a method wants to tell its caller that something has gone wrong, a particular exception class is instantiated and thrown with the throw keyword. For example:
1 2 3 4 5 6 7 | public void foo(String s) { if (s == null) { IllegalArgumentException e = new IllegalArgumentException("s cannot be null"); throw e; } } |
A method can throw an exception at any time, however the types of exception that can be thrown depend on the method signature (See “throws” below).
throws
For a method to throw an exception, that method needs to indicate to the caller that it may possibly be thrown (we’ll see in a future blog that this isn’t always true). In doing so, the calling method is forced by the compiler to do one of two things:
Throw the exception again
For an exception to be thrown by a method, the method must indicate the types of exceptions that can be thrown through the use of the throws keyword. An example method signature demonstrating this is as follows:
1 2 | public void foo() throws IOException, FileNotFoundException |
(note that this can all be on the same line, it’s just split to fit the current blog theme)
In doing so, this method is declaring that anyone who calls foo() must be able to react to exceptions of type IOException or FileNotFoundException.
If the caller does not wish to handle the exception, the caller can simply declare that these exceptions are thrown as well. For instance:
1 2 3 4 | public void bar() throws IOException, FileNotFoundException { foo(); } |
The above example declares that if foo throws one of the two declared exception types, it will be thrown up again to the caller of bar. At that point, the caller of bar must handle the exception.
Handle, or “catch”, the exception
In catching an exception, the caller is attempting to react to the exception and correct the problem. Catching an exception is done through a try/catch block. The format is as follows:
1 2 3 4 5 6 7 | try { foo(); bar(); } catch (Exception e) { // Handle exception } |
The interpretation for the above code is as follows: Attempt to execute the code in the try block. If an exception occurs, immediately stop processing the code in the try block and execute the code in the catch block. Flow then resumes after the catch block.
These two statements are important. If, in the above code, foo() throws an exception, bar() will not be called. Instead, any code in the catch block will be executed and then the flow of the program will continue with the next line after the catch block. If no exception occurs, the code in the catch block is not executed.
Keep in mind that multiple catch blocks may be specified for one try block. Each catch block should catch a different type of exception. Multiple catch blocks are needed if there are multiple exception types that may be thrown by any or all of the methods in the try block. Thus, given the above example, if foo() is declared as throwing IOException and bar() as throwing FileNotFoundException, the try/catch block should appear as follows:
1 2 3 4 5 6 7 8 9 10 | try { foo(); bar(); } catch (IOException e) { // Code to handle IOException } catch (FileNotFoundException e) { // Code to handle FileNotFoundException } |
The next posts in this series will discuss the difference between checked and unchecked exceptions, the rest of the exception object hierarchy including Error and Throwable, and the information included inside of an exception object.

