Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the order in which try catch finally is executed in java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the execution order of try catch finally in java". The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the execution order of try catch finally in java".

Conclusions on the execution sequence of trycatchfinally:

1, regardless of whether there is an exception or not, the code in the final block will execute

2, when there is a return in the attempt and capture, it will still execute in the end

3, finally, it is executed after the later expression operation is returned (at this time, the value after the operation is not returned, but the value to be returned is saved first, regardless of the code in the end, the value returned will not change. It is still the previously saved value), so the return value of the function is determined before the final execution.

4, in the end, do not include the return, otherwise the program will exit ahead of time, and the return value is not the return value saved in the attempt or capture.

An example of the execution sequence of trycatchfinally:

Case 1:try {} catch () {} finally {} is returned

Obviously, the program is executed sequentially.

Case 2: try {return;} catch () {} finally {} return

The program executes the code before tryblock returns (including expression operations in return statements)

Then execute the finally block, and finally execute the try to return

The statement after the last block returns and is no longer executed because the program has already returned in the attempt.

Situation 3:try {} catch () {return;} finally {} return

The program executes the attempt first, and executes the catch block if it encounters an exception

If there is an exception, execute the code before the return in the fishing (including the expression operation in the return statement), and then execute all the code in the last statement.

Finally, the return in the capture block is executed. Finally, the code in 4 places is no longer executed.

No exception: try again after execution and return at last.

Case 4: try {return;} catch () {} finally {return;}

The program executes the code before tryblock returns (including expression operations in return statements)

Then execute the finally block and exit early because there is a return in the finally block.

Situation 5:try {} catch () {return;} finally {return;}

The program executes the code before being returned in the catch block (including the expression operation in the return statement)

Then execute the finally block and exit early because there is a return in the finally block.

Situation 6:try {return;} catch () {return;} finally {return;}

The program executes the code before it is returned in tryblock (including expression operations in return statements).

Exception: execute the code before the return in the catch block (including the expression operation in the return statement)

Then go to the final block and exit early because there is a return in the final block.

No exception: then execute the last block and exit early because there is a return in the final block

Thank you for your reading, the above is the content of "what is the execution order of try catch finally in java". After the study of this article, I believe you have a deeper understanding of what the execution order of try catch finally in java is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

  • How to use html to implement annotations

    This article is about how to use html to implement annotations. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look. The HTML comment begins with

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report