当前位置:网站首页>try-catch-finally
try-catch-finally
2022-06-25 11:20:00 【User 9854323】
public class Main {
public static void main(String[] args) {
System.out.println(" return :"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;
}
}
}
Output :
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
return :6
If changed to the following ( final return sentence ):
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;
}
}
}
Output :
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
return :8
summary : 1、finally There is no return when , After execution try or catch Of the statement return After that, it will be executed finally.( and return The value of will be temporarily stored in the stack , wait for finally After execution, return to ) 2、finally There are return when , Will still carry out try or catch Of the statement return, But the end result is finally Statement return.
边栏推荐
- [maintain cluster case set] gaussdb query user space usage
- Continuous delivery jenkinsfile syntax
- Arrays.asList()
- Use of three-level linkage plug-ins selected by provinces and cities
- Handler、Message、Looper、MessageQueue
- After reading this article, I will teach you to play with the penetration test target vulnhub - drivetingblues-7
- Checking whether the double value is an integer - Swift - checking if a double value is an integer - swift
- Query method and interrupt method to realize USART communication
- Big Endian 和 Little Endian
- How to realize the rich text editor function of mobile terminal
猜你喜欢
Jincang KFS data centralized scenario (many to one) deployment
Explanation and use of kotlin syntax for Android
【文件包含漏洞-03】文件包含漏洞的六种利用方式
Leetcode 1249. 移除无效的括号(牛逼,终于做出来了)
Leetcode 1249. Remove invalid brackets (awesome, finally made)
Coscon'22 lecturer solicitation order
中国信通院沈滢:字体开源协议——OFL V1.1介绍及合规要点分析
牛客网:主持人调度
Writing wechat applet with uni app
Jincang KFS data cascade scenario deployment
随机推荐
SystemVerilog(十三)-枚举数据类型
Android:kotlin中Gson与JSON的泛型映射解析
COSCon'22 讲师征集令
Kingbasees plug-in DBMS of Jincang database_ session
[维护集群案例集] GaussDB 查询用户空间使用情况
FPGA基于VGA显示字符及图片
SQL injection vulnerability (bypass)
Network remote access using raspberry pie
仿真与烧录程序有哪几种方式?(包含常用工具与使用方式)
基于C语言的图书信息管理系统 课程论文+代码及可执行exe文件
Double buffer transparent encryption and decryption driven course paper + project source code based on minifilter framework
Tidb applicable scenarios
Technical practice and development trend of video conference all in one machine
今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界
GC
Daily 3 questions (2) - find out the lucky numbers in the array
每日3题(3)-检查整数及其两倍数是否存在
JVM 原理简介
金仓数据库 KingbaseES 插件ftutilx
Course paper + code and executable EXE file of library information management system based on C language