当前位置:网站首页>try-catch finally执行顺序的例题
try-catch finally执行顺序的例题
2022-06-13 03:39:00 【禹哥。。。】
public class TryDemo {
public static void main(String[] args) {
System.out.println(test());
}
public static int test() {
try {
return 1;
} catch (Exception e) {
return 2;
} finally {
System.out.print("3");
}
}
}
程序先进入到try中,返回1,然后回继续执行finally打印3,函数执行完毕,将1返回给主函数,然后主函数输出1
public static void main(String[] args) {
System.out.println(test1());
}
public static int test1() {
try {
return 2;
} finally {
return 3;
}
}
按道理来说会先返回2,然后进入finally中返回3,但是一个程序只能有一个返回值,finally块是最后执行的,所以返回的是3,最后的结果是3
public static void main(String[] args) {
System.out.println(test1());
}
public static int test1() {
int i = 0;
try {
i = 2;
return i;
} finally {
i = 3;
}
}
进入try中先给i赋值为2,然后将它返回,接着近finally块中将i设置为3,finally中的代码只是给i赋了值,并不会改变返回值中i的值,所以返回的还是2,最后的结果是i
结论:
1、不管有没有出现异常,finally块中代码都会执行;
2、当try和catch中有return时,finally仍然会执行;
3、finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,不管finally中的代码怎么样,返回的值都不会改变,任然是之前保存的值),所以函数返回值是在finally执行前确定的;
4、finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值
边栏推荐
- 单片机外设介绍:温度传感器 DS18B20
- Use of file class
- Complete set of Stata code commands: follow and verify do files, common Stata commands, code collection, panel entropy method
- 无人机避障四种常见技术中,为何大疆首选双目视觉
- Scala sets (array, list, set, map, tuple, option)
- swap()
- What to bring to 2022
- Alibaba cloud keep on record
- Batch image Download & socket dialogue
- 单片机:PCF8591硬件接口
猜你喜欢

【面试复习】自用不定时更新

Spark optimization - data skew solution

2000-2019 enterprise registration data of all provinces, cities and counties in China (including longitude and latitude, registration number and other multi indicator information)

【youcans 的 OpenCV 例程200篇】201. 图像的颜色空间转换

Workflow of driver of spark kernel (stage division, task division, task scheduling)

Prefecture level city - air flow coefficient data - updated to 2019 (including 10m wind speed, boundary height, etc.)

LVS四层负载均衡集群(5)LVS概述

An error is reported in the JDBC connection database: connection to 139.9.130.37:15400 referred

Spark kernel (execution principle) environment preparation /spark job submission process

四旋翼飞行器避障系统基础
随机推荐
【 développement d'essais 】 sélénium d'essais automatisés (Ⅲ) - - analyse du cadre unitest
LVS四层负载均衡集群(4)负载均衡的主要方式
Lambda终结操作查找与匹配noneMatch
单片机:PCF8591 应用程序
单片机:Modbus 通信协议介绍
【测试开发】测试的相关基本概念
User and permission configuration in SQL Server database to ensure database security
LVS four - tier Load Balancing Cluster (5) LVS Overview
LVS 4 - tier Load Balancing Cluster (3) Cluster Function Classification - HPC
【测试开发】自动化测试selenium篇(一)
[multithreading] what is multithreading in the end -- the elementary level of multithreading (review for self use)
年金险产品保险期满之后能领多少钱?
LVS four layer load balancing cluster (5) LVS overview
Lambda终结操作查找与匹配allMatch
Workflow of driver of spark kernel (stage division, task division, task scheduling)
19 MySQL database optimization methods
Doris data import broker load
Several common ways for Flink to extract eventtime and generate watermark
Display line number in MySQL query result
Synching build your own synchronization cloud