当前位置:网站首页>一文搞懂try、catch、finally(包含return)执行流程(全网最详细解析)
一文搞懂try、catch、finally(包含return)执行流程(全网最详细解析)
2022-07-25 09:16:00 【努力努力再努力Gzc.】
一、首先,确定这几个前提咱们先知晓:
1、try块必须搭配catch块或者finally块使用,不可以单独使用
(正常使用方式分为3种:①try+catch;②try+finally;③try+catch+finally)
2、正常运行情况下、不管有没有抛出异常、只要代码中有finally块,那finally块必然会执行,且finally块是在try或catch中的return语句执行之后、返回之前执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,不管finally块中的代码怎么样,只要是没有return语句,那么返回的值都不会改变,仍然是之前try或者catch块中保存的值)。
(注意:在一些非正常情况下,比如在执行finally块之前,虚拟机被终止运行;程序所在线程死亡;cpu被关闭这些情况下,finally块自然就不会执行了)
3、当finally块中有return语句时,不论try和catch中有没有return语句,程序都一定会从finally块中的return语句返回,即返回值也会是finally块中return语句的返回值,而不是try或者catch块中的。
二、 当try、catch、finally、return一起使用时,代码的执行流程详解:
(1)try{} catch(){}finally{} return;
直接按正常顺序执行。
(2)try{ return; }catch(){} finally{} return;
先执行try块中return语句,但是并不返回值;然后再执行finally块中的语句,最后再返回try中return的值,finally块后的return不会执行,因为程序已经在try块的return处返回。
(3) try{ } catch(){return;} finally{} return;
先执行try块,然后再看是否有异常:
有异常的情况下:执行catch块的return但不返回,再执行finally块,最后再返回catach块中的return值。finally后的return不执行。
没有异常的话,按顺序执行finally块再return。
(4)try{ return; }catch(){} finally{return;}
执行try块return语句(包括return语句中的表达式运算),但不返回;
再执行finally块,执行finally块,有return,从这里返回。
此时finally块的return值,就是代码执行完后的值
(5) try{} catch(){return;}finally{return;}
有异常:先执行try,再执行catch中的return但不返回,再执行finally块中的return,从这里返回。
无异常:先执行try,再执行finally块,有return,从这里返回。
(6)try{ return;}catch(){return;} finally{return;}
首先程序执行try块中return语句(包括return语句中的表达式运算),但不返回;然后再看是否有异常
有异常:执行catch块中return语句(包括return语句中的表达式运算),但不返回;
再执行finally块,有return,从这里返回。
无异常:再执行finally块,有return,从这里返回。
三、总结
(1)任何执行try 或者catch中的return语句之后,在返回之前,如果finally存在的话,都会先执行finally语句。
(2)如果finally中有return语句,那么程序就return了,所以finally中的return是一定会被return的,
编译器把finally中的return实现为一个warning。
边栏推荐
- Bi business interview with data center and business intelligence (I): preparation for Industry and business research
- 『怎么用』装饰者模式
- The development of art NFT
- [STL]list模拟实现
- Learn about spark project on Nebula graph
- Arrange the array into the smallest number
- [STL]stack&queue模拟实现
- This ten-year content industry infrastructure company is actually an invisible Web3 pioneer
- registration status: 204
- How to write the code of wechat applet implementation tab
猜你喜欢

Programmers can't SQL? Ashes Engineer: all waiting to be eliminated! This is a must skill!

yarn : 无法加载文件 yarn.ps1,因为在此系统上禁止运行脚本。
![[machine learning] Finally, the important steps of machine learning modeling have been clarified](/img/75/07767ed694502f0c910d35e1447499.jpg)
[machine learning] Finally, the important steps of machine learning modeling have been clarified

mysql中的数据结果排名

activemq--死信队列

『每日一问』简单聊聊JMM/说说对JMM的了解
![[NPM] the](/img/ae/efccefae0323a1f6a425523e01d2ac.png)
[NPM] the "NPM" item cannot be recognized as the name of cmdlets, functions, script files or runnable programs. Please check the spelling of the name. If the path is included, make sure the path is co

2022-7-14 JMeter pressure test

Sort out Huawei ap-3010dn_ V2 configuration create WiFi

Ranking of data results in MySQL
随机推荐
NFT guide for musicians
c语言中的六个存储类型:auto register static extern const volatile
通过robocopy对文件/夹进行复制
Disable module (attribute node) in LabVIEW
What is steel grating?
This ten-year content industry infrastructure company is actually an invisible Web3 pioneer
activemq--可持久化机制之JDBC代码
The operation cannot be completed because a folder or file in it is already open in another program
ActiveMQ -- AMQ of persistent mechanism
SSM高级整合
registration status: 204
Redis的十大常见应用场景
Sort out Huawei ap-3010dn_ V2 configuration create WiFi
Activemq-- asynchronous delivery
Anti shake and throttling
[C language] dynamic memory management, flexible array
Silicon Valley classroom lesson 12 - official account on demand course and live broadcast management module
The annualization of financial products is 4%. How much profit can you get from buying 10000 yuan a month?
什么是单机、集群与分布式?
Ten thousand words long, one word thoroughly! Finally, someone has made business intelligence (BI) clear