Try, catch and finally

  • Try block: Used to catch exceptions.
    • It can be followed by zero or more catch blocks.
    • There can only be zero or one finally block.
    • A try block must be followed by a finally block if there is no catch block.
    • After the exception is caught by the executing code, the catch block is entered, and the code after the exception occurs in the try does not continue to execute.
  • Catch block: Used to handle exceptions caught in a try.
    • There can be more than one catch block. After a catch block is executed, a finally block is entered if there is a finally block. Even if there is a catch block after it, it will never enter another catch block.
  • Finally block: Code ina finally block is executed whether or not an exception is caught or handled.
    • When a return statement is encountered ina try or catch block, the finally code is executed before the return statement is executed.

There can be more than one catch block, and only zero or one finally block after a try block

public static void main(String[] args) { try { System.out.println("try..." ); }catch (ArithmeticException e){ System.out.println("ArithmeticException..." ); }catch (NullPointerException e){ System.out.println("NullPointerException..." ); } finally { System.out.println("finally..." ); }} // Output: //try... //finally...Copy the code

A try block must be followed by a finally if there is no catch block

public static void main(String[] args) { try { System.out.println("try..." ); } finally { System.out.println("finally..." ); }} // Output: //try... //finally...Copy the code

After the exception is caught by the executing code, the catch block is entered, and the code after the exception occurs in the try does not continue to execute

public static void main(String[] args) { try { System.out.println("try..." ); int a = 0; String str = null; System.out.println(str.toString()); a = a / 0; } catch (ArithmeticException e) { System.out.println("ArithmeticException..." ); } catch (NullPointerException e) { System.out.println("NullPointerException..." ); } finally { System.out.println("finally..." ); }} // Output: //try... //NullPointerException... //finally...Copy the code

When a return statement is encountered ina try or catch block, the finally code is executed before the return statement is executed.

public static void main(String[] args) { try { System.out.println("try..." ); return; } catch (ArithmeticException e) { System.out.println("ArithmeticException..." ); } catch (NullPointerException e) { System.out.println("NullPointerException..." ); } finally { System.out.println("finally..." ); }} // Output: //try... //finally...Copy the code
public static void main(String[] args) { try { System.out.println("try..." ); int a = 0; a = a / 0; } catch (ArithmeticException e) { System.out.println("ArithmeticException..." ); return; } catch (NullPointerException e) { System.out.println("NullPointerException..." ); } finally { System.out.println("finally..." ); }} // Output: //try... //ArithmeticException... //finally...Copy the code