Table of Contents

Exception Handling

The exception handling features in the Java language provide a mechanism for gracefully recovering from a wide variety of unexpected situations in your code.

Some methods handle error conditions by returning an error code. For example, exists() method of the java.io.File class returns false if the file does not exist. However, this isn't good enough for us.

Exceptions

int x=Integer.parseInt("1.234");

Consider the following code snippet:

String input = JOptionPane.showInputDialog(null, "How old are you (in years)");
int age;
try {
  age = Integer.parseInt(input);
  JOptionPane.showMessageDialog(null, "I'm guessing you'll be " + (age+1) + " next year");
} catch (NumberFormatException e) {
  JOptionPane.showMessageDialog(null, "Error!  You should enter an integer value");
}

If we enter something other than an integer, the program will display the error message, otherwise it will “guess” how old the user will be next year.

To summarize…

try block

catch blocks

Types of Exceptions

Brief Subclasses Overview

We will discuss subclasses more in the future, but, in short, a subclass is a specialization of a class. By way of analogy, a Dog is a type of animal.

Types of Exceptions revisited

Looking again at Throwable, Error, and Exception we can now conclude:

Handling Exceptions

catch (Exception e) {
  // ...
}

since Exception is a superclass of NumberFormatException.

Recapping

Consider the following code:

String input = JOptionPane.showInputDialog(null, "How old are you (in years)");
int age;
try {
  age = Integer.parseInt(input);
  JOptionPane.showMessageDialog(null, "I'm guessing you'll be " + (age+1) + " next year");
} catch (NumberFormatException e) {
  JOptionPane.showMessageDialog(null, "Error!  You should enter an integer value");
} catch (RuntimeException e) {
  JOptionPane.showMessageDialog(null, "Error!  Some runtime exception other than NumberFormatException was thrown");
  JOptionPane.showMessageDialog(null, "Hint: " + e.getMessage());
  return;
}
 
JOptionPane.showMessageDialog(null, "No worries.  Either way we'll continue on with our program");

finally block

String input = JOptionPane.showInputDialog(null, "How old are you (in years)");
int age;
try {
  age = Integer.parseInt(input);
  JOptionPane.showMessageDialog(null, "I'm guessing you'll be " + (age+1) + " next year");
} catch (NumberFormatException e) {
  JOptionPane.showMessageDialog(null, "Error!  You should enter an integer value");
} catch (RuntimeException e) {
  JOptionPane.showMessageDialog(null, "Error!  Some runtime exception other than NumberFormatException was thrown");
  JOptionPane.showMessageDialog(null, "Hint: " + e.getMessage());
  return;
} finally {
  JOptionPane.showMessageDialog(null, "I really want you to see this.");
}
 
JOptionPane.showMessageDialog(null, "No worries.  Either way we'll continue on with our program");

throw statement

throw new Exception("Info about the exception");

Modifying the previous code to throw an exception if the user entered an age greater than 120 would look like this:

String input = JOptionPane.showInputDialog(null, "How old are you (in years)");
int age;
try {
  age = Integer.parseInt(input);
  if(age>120)
  {
    throw new Exception("Out of bounds");
  }
  JOptionPane.showMessageDialog(null, "I'm guessing you'll be " + (age+1) + " next year");
} catch (NumberFormatException e) {
  JOptionPane.showMessageDialog(null, "Error!  You should enter an integer value");
} catch (RuntimeException e) {
  JOptionPane.showMessageDialog(null, "Error!  Some runtime exception other than NumberFormatException was thrown");
  JOptionPane.showMessageDialog(null, "Hint: " + e.getMessage());
  return;
} finally {
  JOptionPane.showMessageDialog(null, "I really want you to see this.");
}
 
JOptionPane.showMessageDialog(null, "No worries.  Either way we'll continue on with our program");

Exception throwers

Unchecked Exception

void someMethod() throws IOException {
  // ...
}