Next: Variables in try clauses. Up: More on exceptions. Previous: String parsing exceptions.


Multiple catch blocks

Occassionally, you may run into a situation where you want multiple catch blocks for the same try block. You can get around this by stringing the catch blocks together, as the following illustrates.

try {
    IO.println("Type two numbers to divide.");
    int m = Integer.parseInt(IO.readLine());
    int n = Integer.parseInt(IO.readLine());
    IO.println(m / n);
} catch(NumberFormatException e) {
    IO.println("That's not a number.");
} catch(ArithmeticException e) {
    IO.println("I can't divide by zero.");
}

Next: Variables in try clauses. Up: More on exceptions. Previous: String parsing exceptions.