(Please use your browser's Back button to return)

What is an exception?

When are exception objects created?

What happens once an exception object is created?

Is a method I write required to catch an exception thrown by a method it calls?

How will I know whether a method I am calling requires me to catch to re-throw an exception?

Enough questions and lists! I need some examples!

Glad to oblige. Here are examples of:
  1. catching an exception thrown by a called method
  2. re-throwing (propagating) an exception thrown by a called method
  3. a method that creates and throws an exception
  4. a method that creates and throws a customized exception
  5. a method that both catches and throws an exception

Catching an exception thrown by a called method

/*
 *  Method to that simply returns true if given file exists and false otherwise
 */
 public boolean fileExists(String fileName) {
    try {
       FileReader myReader = new FileReader(fileName);
    } catch (FileNotFoundException e) {
       return false;
    }
    return true;
 }
Filereader is a class in the java.io package. The constructor used here is defined to throw a FileNotFoundException (check the documentation). Notice that the method call must occur in what is called a try block. Notice also that the catch is written with a parameter list containing one parameter: the exception object e.


Re-throwing (propagating) an exception thrown by a called method

/*
 *  Method that returns value read from an existing FileReader
 */
 public int getValue(FileReader myReader) throws IOException {
    return myReader.read();
 }
The read() method that FileReader inherits from InputStreamReader is defined to throw an IOException if any error occurs during the read operation (check the documentation). If your method is not going to catch the exception, it must be defined to throw it as shown here with the throws IOException clause.


A method that creates and throws an exception

/*
 *  Method to yield a secret code, provided the supplied password is correct.
 *  If the password is incorrect, it throws a general Exception.
 */
 public int getCode(String password) throws Exception {
    if (password.equals("where's my code?")) {
       return 8397201;
    } else {
       throw new Exception("Invalid password: "+password);
    }
 }
Notice the method header must declare it may potentially throw an exception of type Exception. Also notice that the Exception object is created and thrown in one statement, and that a message explaining the exception can be passed to the Exception constructor.


A method that creates and throws a customized exception

/*
 *  Customized exception subclass, which must be stored in file BadPasswordException.java
 */
 public class BadPasswordException extends Exception {
    public BadPasswordException(String badPassword) {
       super("Invalid password: "+badPassword);
    }
 }

/*
 *  Method to yield a secret code, provided the supplied password is correct.
 *  If the password is incorrect, it throws a BadPasswordException.
 */
 public int getSecretCode(String password) throws BadPasswordException {
    if (password.equals("where's my code?")) {
       return 8397201;
    } else {
       throw new BadPasswordException(password);
    }
 }
Notice that the definition of the BadPasswordException class must be in a separate file called BadPasswordException.java. This is of course true for any public Java class.


A method that both catches and throws an exception

/*
 *  Method that stores and returns value read from an existing FileReader
 *  If an IOException occurs, store MAX_VALUE then propagate the exception.
 */
 public int setAndGetValue(FileReader myReader) throws IOException {
    try {
       this.value = myReader.read();
    } catch (IOException e) {
       this.value = Integer.MAX_VALUE;
       throw e;
    }
    return this.value;
 }
This technique is used when you want to propagate the exception to your client but still do some cleanup before leaving. Notice the thrown exception is the same object that was just caught. It is possible to throw a different exception object, or an object of a different exception type as well (the throws clause would have to match or be type-compatible with the exception object type).


I think I understand the basics.

These examples cover the basic situations that arise, and are enough for an overview. The design choices made concerning exceptions are usually based on the contractual relationship between the supplier (author) of a class and its clients.