当前位置:网站首页>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.
边栏推荐
- Network principle (2) -- network development
- Activemq-- delayed delivery and scheduled delivery
- Click to hide the column in wechat applet, and then click to show it
- c语言中的六个存储类型:auto register static extern const volatile
- PHP网站设计思路
- redis操作利用游标代替keys
- Activemq-- asynchronous delivery
- Two Sum
- ActiveMQ -- leveldb of persistence mechanism
- centos更改mysql数据库目录
猜你喜欢

Unable to start debugging on the web server, the web server failed to find the requested resource

activemq--可持久化机制之JDBC的journal

activemq--可持久化机制之JDBC代码

分布式一致性协议之Raft

『怎么用』代理模式

ActiveMQ -- message retry mechanism

TCP网络应用程序开发流程

Wechat applet obtains the data of ---- onenet and controls the on-board LED of STM32

Nacos启动报错Unable to start web server
![[SCADA case] myscada helps VIB company realize the modernization and upgrading of production line](/img/67/b8c397d78a675014b5e08ceefc88dc.png)
[SCADA case] myscada helps VIB company realize the modernization and upgrading of production line
随机推荐
Two Sum
Mongodb installation and use
『怎么用』代理模式
DVWA练习一 暴力破解
< T> Generic method demonstration
sqli-labs安装 环境:ubuntu18 php7
在Ubuntu中安装MySQL并创建新用户
MySQL takes the query result as the data updated by update, and concatenates it after the original field data (Lej)
Common tool classes under JUC package
多态和接口
ActiveMQ -- leveldb of persistence mechanism
ActiveMQ -- kahadb of persistent mechanism
ActiveMQ -- JDBC Journal of persistent mechanism
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
Redis operation uses cursor instead of keys
registration status: 204
ActiveMQ -- persistent mechanism
activemq--可持久化机制之JDBC代码
『每日一问』LockSupport怎么实现线程等待、唤醒
Deep understanding of static keyword