当前位置:网站首页>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。
边栏推荐
- [file inclusion vulnerability-04] classic interview question: how to getshell when a website is known to have only local file inclusion vulnerability?
- TASK03|概率论
- keep-alive
- Sign up to open the third session of the "flying oar hacker marathon". It's been a long time
- 过拟合原因及解决
- Use of three-level linkage plug-ins selected by provinces and cities
- 今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界
- 基于OpenStreetMap+PostGIS的地理位置系统 论文文档+参考论文文献+项目源码及数据库文件
- 报名开启|飞桨黑客马拉松第三期如约而至,久等啦
- MySQL synchronous data configuration and shell script implementation
猜你喜欢

A five-year technical Er, based on the real experience of these years, gives some suggestions to the fresh students

Chinese translation of IMA Lazarus' the new giant, the goddess of Liberty

Network remote access using raspberry pie

中国信通院沈滢:字体开源协议——OFL V1.1介绍及合规要点分析

scrapy+scrapyd+gerapy 爬虫调度框架

What are the ways to simulate and burn programs? (including common tools and usage)

Android之Kotlin语法详解与使用

数据库系列:MySQL索引优化总结(综合版)

At 16:00 today, Mr. sunxiaoming, a researcher of the Institute of computing, Chinese Academy of Sciences, took you into the quantum world

网易开源的分布式存储系统 Curve 正式成为 CNCF 沙箱项目
随机推荐
今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界
Shen Ying, China Academy of communications and communications: font open source protocol -- Introduction to ofl v1.1 and analysis of key points of compliance
Kingbasees plug-in ftutilx of Jincang database
MySQL synchronous data configuration and shell script implementation
金仓数据库 KingbaseES 插件DBMS_UTILITY
Remove the problem of orange border on the desktop control in WebView
GaussDB others内存比较高的场景
反应c语言程序结构特点的程序
Android之Kotlin语法详解与使用
Checking whether the double value is an integer - Swift - checking if a double value is an integer - swift
每日3题(3)-检查整数及其两倍数是否存在
金仓数据库 KingbaseES 插件DBMS_OUTPUT
【上云精品】节能提效!加速纺织业“智造”转型
TASK03|概率论
Apache ShenYu 入门
数据库系列:MySQL索引优化总结(综合版)
[file inclusion vulnerability-04] classic interview question: how to getshell when a website is known to have only local file inclusion vulnerability?
Android:kotlin中Gson与JSON的泛型映射解析
Es learning
Multiple environment variables