Skip to content

Catch Exception

In Java, any statement that may throw an exception can be caught with try ... catch . Put statements that may cause exceptions in try { ... } , and then use catch to capture the corresponding Exception and its subclasses.

Multiple catch statements

Multiple catch statements can be used, and each catch captures the corresponding Exception and its subclasses. After the JVM catches the exception, it will match the catch statement from top to bottom. After matching a certain catch , it will execute the catch code block and then no longer continue to match.

Simply put: only one of multiple catch statements can be executed. For example:

java
public static void main(String[] args) {
    try {
        process1();
        process2();
        process3();
    } catch (IOException e) {
        System.out.println(e);
    } catch (NumberFormatException e) {
        System.out.println(e);
    }
}

When there are multiple catch , the order of catch is very important: subclasses must be written first. For example:

java
public static void main(String[] args) {
    try {
        process1();
        process2();
        process3();
    } catch (IOException e) {
        System.out.println("IO error");
    } catch (UnsupportedEncodingException e) { // Never caught
        System.out.println("Bad encoding");
    }
}

For the above code, UnsupportedEncodingException exception can never be caught because it is a subclass of IOException . When an UnsupportedEncodingException exception is thrown, it will be caught and executed by catch (IOException e) { ... } .

Therefore, the correct way to write it is to put the subclass first:

java
public static void main(String[] args) {
    try {
        process1();
        process2();
        process3();
    } catch (UnsupportedEncodingException e) {
        System.out.println("Bad encoding");
    } catch (IOException e) {
        System.out.println("IO error");
    }
}

finally statement

Regardless of whether an exception occurs, if we want to execute some statements, such as cleanup work, how to write it?

You can write the execution statement several times: put it in try for normal execution, and write it again for each catch . For example:

java
public static void main(String[] args) {
    try {
        process1();
        process2();
        process3();
        System.out.println("END");
    } catch (UnsupportedEncodingException e) {
        System.out.println("Bad encoding");
        System.out.println("END");
    } catch (IOException e) {
        System.out.println("IO error");
        System.out.println("END");
    }
}

The above code will execute System.out.println("END"); statement regardless of whether an exception occurs.

So how do you eliminate these duplicate codes? Java's try ... catch mechanism also provides a finally statement. The finally statement block guarantees that it will be executed whether there is an error. The above code can be rewritten as follows:

java
public static void main(String[] args) {
    try {
        process1();
        process2();
        process3();
    } catch (UnsupportedEncodingException e) {
        System.out.println("Bad encoding");
    } catch (IOException e) {
        System.out.println("IO error");
    } finally {
        System.out.println("END");
    }
}

Note finally has several characteristics:

  • The finally statement is not necessary and can be written or not;
  • finally is always executed last.

If no exception occurs, execute try { ... } statement block normally, and then execute finally . If an exception occurs, execution of try { ... } statement block is interrupted, then jumps to the matching catch statement block, and finally executes finally .

It can be seen finally is used to ensure that some code must be executed.

In some cases, there is no catch and only try ... finally structure is used. For example:

java
void process(String file) throws IOException {
    try {
        ...
    } finally {
        System.out.println("END");
    }
}

Because the method declares the exceptions that may be thrown, there is no need to write catch .

Catching various exceptions

If the handling logic of some exceptions is the same, but the exceptions themselves do not have an inheritance relationship, then you have to write multiple catch clauses:

java
public static void main(String[] args) {
    try {
        process1();
        process2();
        process3();
    } catch (IOException e) {
        System.out.println("Bad input");
    } catch (NumberFormatException e) {
        System.out.println("Bad input");
    } catch (Exception e) {
        System.out.println("Unknown error");
    }
}

Because the code for handling IOException and NumberFormatException is the same, we can | it together:

java
public static void main(String[] args) {
    try {
        process1();
        process2();
        process3();
    } catch (IOException | NumberFormatException e) {
        // IOException或NumberFormatException
        System.out.println("Bad input");
    } catch (Exception e) {
        System.out.println("Unknown error");
    }
}

Summary

When catching exceptions, the matching order of multiple catch statements is very important, and subclasses must be placed first;

The finally statement ensures that it will be executed whether there is an exception, and it is optional;

A catch statement can also match multiple non-inherited exceptions.

Catch Exception has loaded