EMZETT.
Login

Multiple Exceptions

In short: The ability to react differently (or also jointly) to several different types of error within a single error-handling block.

In more detail: Instead of writing a completely separate try block for every possible error type, several catch branches can be attached to a single try block — each responsible for a different exception type. Some languages additionally allow combining several error types into a single catch branch, when they should be handled the same way.

In Depth

try {
    processFile(path);
} catch (FileNotFoundException e) {
    System.out.println("File does not exist: " + path);
} catch (IOException e) {
    System.out.println("Read error: " + e.getMessage());
} catch (Exception e) {
    System.out.println("Unexpected error: " + e.getMessage());
}

An important detail about the order of catch branches: most languages check them from top to bottom and take the FIRST matching one — which is why more specific error types have to come BEFORE more general ones. If a general catch (Exception e) is the first branch, it also catches all more specific error types, and the more specific catch branches below it are never reached (in many languages, this is even a compile error, because the code is provably unreachable).

Some languages (e.g. Java from version 7) additionally allow combining several error types into one branch with |, when the same handling should apply to both:

catch (FileNotFoundException | SecurityException e) {
    System.out.println("Access not possible: " + e.getMessage());
}

This avoids duplicated code when two different causes of error (file doesn’t exist vs. no permission) should be handled the same way, but still remain content-wise different, named error types (unlike a blanket catch (Exception e), which loses any distinction).

See also: Exceptions, Error Codes