Exceptions

Start Prev Next

Exceptions provide an error handling mechanism

try {
    System.out.println(a/b);
} catch (ArithmeticException e) {
    System.out.println("Had a problem!");
}
Exceptions propagate upwards until a catch block gets them

Can throw exceptions:

throw new ArithmeticException();
Can have multiple catch blocks:
try {
} catch (IOException e) {
} catch (ArithmeticException e) {
} catch (Exception e) { // most general exception
}
Methods must specify exceptions they throw
public Foo() throws IOException { ... }