In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un