当前位置:网站首页>Understand the execution process of try, catch and finally (including return) (the most detailed analysis of the whole network)
Understand the execution process of try, catch and finally (including return) (the most detailed analysis of the whole network)
2022-07-25 09:26:00 【Work hard, work hard, gzc】
One 、 First , To determine these prerequisites, let's know :
1、try Blocks must match catch Block or finally Block use , It can't be used alone
( The normal use mode is divided into 3 Kind of :①try+catch;②try+finally;③try+catch+finally)
2、 Under normal operation 、 Whether or not an exception is thrown 、 As long as the code has finally block , that finally Blocks are bound to execute , And finally Block is in try or catch Medium return After statement execution 、 Return to the previous execution ( The calculated value is not returned , Instead, save the value you want to return , No matter finally How about the code in the block , As long as there is no return sentence , Then the returned value will not change , Still before try perhaps catch The value stored in the block ).
( Be careful : Under some abnormal circumstances , For example, in the implementation of finally Before the block , The virtual machine is terminated ; The thread where the program is located dies ;cpu Be closed in these cases ,finally Naturally, the block will not be executed )
3、 When finally In the block return When the sentence is , Regardless of try and catch Is there a return sentence , The program will start from finally In the block return Statement returns , That is, the return value will also be finally In block return The return value of the statement , instead of try perhaps catch In the block .
Two 、 When try、catch、finally、return When used together , The implementation process of the code is explained in detail :
(1)try{} catch(){}finally{} return;
Execute directly in the normal order .
(2)try{ return; }catch(){} finally{} return;
Execute first try In block return sentence , But it does not return a value ; And then execute finally Statement in block , And finally back to try in return Value ,finally After the block return Not execute , Because the program is already try The block return Back to .
(3) try{ } catch(){return;} finally{} return;
Execute first try block , Then see if there is any abnormality :
In case of abnormality : perform catch The block return But not back , Re execution finally block , And finally back to catach In the block return value .finally After return Don't execute .
If there is nothing unusual , Execute in order finally Block Zai return.
(4)try{ return; }catch(){} finally{return;}
perform try block return sentence ( Include return Expression operations in statements ), But not back ;
Re execution finally block , perform finally block , Yes return, Back from here .
here finally The block return value , After the code is executed
(5) try{} catch(){return;}finally{return;}
There are abnormal : Execute first try, Re execution catch Medium return But not back , Re execution finally In the block return, Back from here .
No abnormality : Execute first try, Re execution finally block , Yes return, Back from here .
(6)try{ return;}catch(){return;} finally{return;}
First, the program executes try In block return sentence ( Include return Expression operations in statements ), But not back ; Then see if there is any abnormality
There are abnormal : perform catch In block return sentence ( Include return Expression operations in statements ), But not back ;
Re execution finally block , Yes return, Back from here .
No abnormality : Re execution finally block , Yes return, Back from here .
3、 ... and 、 summary
(1) Any implementation try perhaps catch Medium return After statement , Before returning , If finally exist , Will execute first finally sentence .
(2) If finally There is return sentence , So the program is return 了 , therefore finally Medium return Is bound to be return Of ,
The compiler the finally Medium return Implemented as a warning.
边栏推荐
- OverTheWire-Bandit
- 数据控制语言(DCL)
- 实现简单的RESTful API服务器
- sqli-labs安装 环境:ubuntu18 php7
- Guangzhou has carried out in-depth "100 day action" to check the safety of self built commercial houses, and more than 2 million houses have been checked in two months
- sqli-labs Basic Challenges Less11-22
- 前台页面打印
- @Scheduled源码解析
- 通过robocopy对文件/夹进行复制
- Common tool classes under JUC package
猜你喜欢

Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL

『怎么用』装饰者模式

Uniapp intercepts route jumps through addinterceptor to control whether the page needs to log in

SSM框架整合,简单案例
![[C language] dynamic memory management, flexible array](/img/da/b9455885df0cb6646908e3655d62c5.png)
[C language] dynamic memory management, flexible array

mysql中的数据结果排名

什么是单机、集群与分布式?

Shell脚本

Shell script
![[GYCTF2020]Node Game](/img/8d/7e6c2fb2a0359298fbcc1cd8544710.png)
[GYCTF2020]Node Game
随机推荐
『怎么用』观察者模式
The interviewer asked: how to prevent oversold? There are several ways to achieve it
Silicon Valley classroom lesson 15 - Tencent cloud deployment
Mongodb installation and use
TCP网络应用程序开发流程
[C language] dynamic memory management, flexible array
How can technologists start their personal brand? Exclusive teaching of top five KOLs
Jspdf generates PDF files. There is a problem of incomplete files. Files are downloaded in the background, but not in the foreground
Mongodb exploration phase [easy to understand]
ActiveMQ -- kahadb of persistent mechanism
『怎么用』装饰者模式
【Nacos】NacosClient在服务注册时做了什么
Comparison between symmetric encryption and asymmetric encryption
Numpy- array属性、改变形状函数、基本运算
API健康状态自检
Interviewer: tell me the difference between redis and mongodb? [easy to understand]
ActiveMQ -- JDBC Journal of persistent mechanism
redis操作利用游标代替keys
activemq--可持久化机制之AMQ
activemq--死信队列