当前位置:网站首页>try-catch-finally
try-catch-finally
2022-06-25 10:58:00 【用户9854323】
public class Main {
public static void main(String[] args) {
System.out.println("返回:"WithException());
}
public static int WithException() {
int i = 10;
try {
System.out.println("i in try block is : " + i);
i = i / 0;
return --i;
} catch (Exception e) {
System.out.println("i in catch - form try block is : " + i);
--i;
System.out.println("i in catch block is : " + i);
return --i;
} finally {
System.out.println("i in finally - from try or catch block is--" + i);
--i;
System.out.println("i in finally block is--" + i);
return --i;
}
}
}
输出:
i in try block is : 10
i in catch - form try block is : 10
i in catch block is : 9
i in finally - from try or catch block is--8
i in finally block is--7
返回:6
如果改为下面(最后的return语句):
public class Main {
public static void main(String[] args) {
System.out.println(WithException());
}
public static int WithException() {
int i = 10;
try {
System.out.println("i in try block is : " + i);
i = i / 0;
return --i;
} catch (Exception e) {
System.out.println("i in catch - form try block is : " + i);
--i;
System.out.println("i in catch block is : " + i);
return --i;
} finally {
System.out.println("i in finally - from try or catch block is--" + i);
--i;
System.out.println("i in finally block is--" + i);
// return --i;
}
}
}
输出:
i in try block is : 10
i in catch - form try block is : 10
i in catch block is : 9
i in finally - from try or catch block is--8
i in finally block is--7
返回:8
总结: 1、finally语句中没有return时,执行完try或catch语句的return之后还会执行finally。(而return的值会暂存在栈里面,等待finally执行后再返回) 2、finally语句中有return时,依然会执行try或catch语句的return,但是最终返回的是finally语句中的return。
边栏推荐
- 基于OpenStreetMap+PostGIS的地理位置系统 论文文档+参考论文文献+项目源码及数据库文件
- Cdn+cos ultra detailed steps for drawing bed construction
- 【观察】ObjectScale:重新定义下一代对象存储,戴尔科技的重构与创新
- Double tampon transparent cryptage et décryptage basé sur le cadre minifilter
- 动态规划解决股票问题(上)
- 数组结构整理
- CSRF attack
- At 16:00 today, Mr. sunxiaoming, a researcher of the Institute of computing, Chinese Academy of Sciences, took you into the quantum world
- Checking whether the double value is an integer - Swift - checking if a double value is an integer - swift
- Spannable 和 Editable、SpannableString 和 SpannableString
猜你喜欢
Complete steps for a complete Oracle uninstall
中國信通院沈瀅:字體開源協議——OFL V1.1介紹及合規要點分析
Use of three-level linkage plug-ins selected by provinces and cities
NuxtJS实战案例
Dragon Book tiger Book whale Book gnawing? Try the monkey book with Douban score of 9.5
Double tampon transparent cryptage et décryptage basé sur le cadre minifilter
c盘使用100%清理方法
At 16:00 today, Mr. sunxiaoming, a researcher of the Institute of computing, Chinese Academy of Sciences, took you into the quantum world
Apache ShenYu 入門
[file inclusion vulnerability-04] classic interview question: how to getshell when a website is known to have only local file inclusion vulnerability?
随机推荐
Five types of questions about network planning
Jincang KFS data cascade scenario deployment
Is it safe to speculate in stocks by mobile phone?
Some assembly instructions specific to arm64
金仓数据库 KingbaseES 插件dbms_session
反应c语言程序结构特点的程序
数组结构整理
基於Minifilter框架的雙緩沖透明加解密驅動 課程論文+項目源碼
金仓数据库 KingbaseES 插件identity_pwdexp
16 种企业架构策略
【上云精品】节能提效!加速纺织业“智造”转型
Dell technology performs the "fast" formula and plays ci/cd
Multiple environment variables
MySQL synchronous data configuration and shell script implementation
Handling of NPM I installation problems
Jincang database kingbasees plug-in identity_ pwdexp
Google Earth engine (GEE) - evaluate enables one click batch download of all single images in the research area (some areas in Shanghai)
网络远程访问的方式使用树莓派
金仓KFS数据集中场景(多对一)部署
Google Earth Engine (Gee) - evaluate réalise le téléchargement en un clic de toutes les images individuelles dans la zone d'étude (certaines parties de Shanghai)