Try block:Used to capture exceptions. Then, zero or multiple catch blocks can be connected. If no Catch Block exists, it must be followed by a Finally block.
Catch Block:It is used to handle exceptions captured by try.
Finally block:The statements in the Finally block are executed no matter whether exceptions are captured or processed. When a return statement is encountered in a try block or catch block, the finally statement block will be executed before the method returns. In the following four special cases, finally blocks are not executed:
1) An exception occurred in the finally statement block.
2) in the previous Code, system. Exit () is used to exit the program.
3) The thread where the program is located is killed.
4) disable the CPU.
3. Try-catch-finally rule (
Syntax rules for exception handling statements
):
1) You must add catch or finally blocks after try. Try block can be followed by both catch and finally blocks, but there must be at least one block.
2) The Block sequence must be followed: if the Code uses both catch and finally blocks, the catch block must be placed after the try block.
3) The Catch Block is related to the type of the corresponding exception class.
4) A try block may have multiple catch blocks. If so, the first matching block is executed. That is, the Java Virtual Machine will match the exception objects that are actually thrown with the exception types declared in each catch code block in sequence. If the exception object is of a certain exception type or its subclass instance, it will execute this catch code block, no other catch code blocks will be executed
5) the try-catch-Finally structure can be nested.
6) in the try-catch-Finally structure, you can throw an exception again.
7) in addition to the following cases, the finally execution is always ended: early termination of JVM (calling system. exit (INT); An unhandled exception is thrown in the Finally block; the computer is powered off, caught fire, or suffers a virus attack.
4. execution sequence of try, catch, and finally statement blocks:
1) when the try clause fails to catch an exception: when the statements in the try clause block are executed one by one, the program will skip the catch statement block and execute the finally statement block and its subsequent statements;
2) When a try captures an exception, the catch statement block does not handle the exception: when a statement in the try statement block encounters an exception, when the catch statement block does not handle this exception, the exception will be thrown to the JVM for processing, and the statements in the finally statement block will still be executed, however, the statement after the finally statement block is not executed;
3) When a try detects an exception, the catch statement block can handle this exception: it is executed in order in the try statement block, when an exception occurs when a statement is executed, the program jumps to the catch statement block and matches the catch statement block one by one to find the corresponding processing program, other catch statement blocks will not be executed, and the statements in the try statement blocks will not be executed even if an exception occurs. After the catch statement block is executed, execute the statements in the finally statement block, and finally execute the statements after the finally statement block;
Try catch finally