前言
由于总是搞不清楚try/catch中的一个执行顺序,返回结果。所以总结一下

1.finally没有return 时,可以看出finally确实在return之前执行了
public static void main(String[] args) {
int aa = test1();
System.out.println(aa);
}

public static int test1(){
try{
System.out.println("try");
return 0;
}catch(Exception e){
System.out.println("catch");
return 1;
}finally {
System.out.println("finally");

}
}

//结果
//try
//finally
//0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2. finally有return 时,会覆盖其他语句中的return
public static void main(String[] args) {
int aa = test1();
System.out.println(aa);
}

public static int test1(){
try{
System.out.println("try");
return 0;
}catch(Exception e){
System.out.println("catch");
return 1;
}finally {
System.out.println("finally");
return 2;
}
}

//结果
//try
//finally
//2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
3.finally中对基本数据类型没有影响
public static int test1(){
int result = 6;
try{
System.out.println("try");
return result;
}catch(Exception e){
System.out.println("catch");
return 1;
}finally {
System.out.println("finally");
result = 3;
}
}

//结果try
//finally
//6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
4.finally中对引用型数据有影响
public static StringBuffer test1(){
StringBuffer str = new StringBuffer("I");
try{
System.out.println("try");
return str;
}catch(Exception e){
System.out.println("catch");
return null;
}finally {
System.out.println("finally");
str.append("am");
}
}

//结果try
//finally
// I am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
5.当try/catch外面有异常,finally不执行
public static int test2(){
int a = 5/0;
try{
System.out.println("try");
return a;
}catch(Exception e){
System.out.println("catch");
return 2;
}finally {
System.out.println("finally");
}
}
1
2
3
4
5
6
7
8
9
10
11
12

Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.jxl.face.Controller.EnumTest.test2(EnumTest.java:29)
at com.jxl.face.Controller.EnumTest.main(EnumTest.java:19)
1
2
3
4
6.异常在try/catch里面,finally无return
public static int test2(){

try{
int a = 5/0;
System.out.println("try");
return a;
}catch(Exception e){
System.out.println("catch");
return 2;
}finally {
System.out.println("finally");
}
}

//catch
//finally
//2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
7.异常在try/catch里面,finally有return
public static int test2(){

try{
int a = 5/0;
System.out.println("try");
return a;
}catch(Exception e){
System.out.println("catch");
return 2;
}finally {
System.out.println("finally");
return 3;
}
}

//catch
//finally
//3

try/catch中finally的执行时间的更多相关文章

  1. C#:在catch中return,会执行finally吗?

    本文转自 vipxiaotian(CSDN) 请参考下面一段简单的语句块: 1:  try2:  {3:      throw new Exception("new exception&qu ...

  2. aspx页面使用ajax遇到try catch中使用Response.End()报错

    1.使用Ajax接收数据,在返回Response.Write()后应该调用Response.End()才能将数据写入到调用的页面,才能被jQuery的回调函数获取到返回的JSON数据 2.在try-- ...

  3. SQL Server中事务transaction如果没写在try catch中,就算中间语句报错还是会提交

    假如我们数据库中有两张表Person和Book Person表: CREATE TABLE [dbo].[Person]( ,) NOT NULL, ) NULL, ) NULL, [CreateTi ...

  4. spring事务——try{...}catch{...}中事务不回滚的几种处理方式

    当希望在某个方法中添加事务时,我们常常在方法头上添加@Transactional注解 @ResponseBody @RequestMapping(value = "/payment" ...

  5. 详解C#泛型(二) 获取C#中方法的执行时间及其代码注入 详解C#泛型(一) 详解C#委托和事件(二) 详解C#特性和反射(四) 记一次.net core调用SOAP接口遇到的问题 C# WebRequest.Create 锚点“#”字符问题 根据内容来产生一个二维码

    详解C#泛型(二)   一.自定义泛型方法(Generic Method),将类型参数用作参数列表或返回值的类型: void MyFunc<T>() //声明具有一个类型参数的泛型方法 { ...

  6. Java的finally语句在try或catch中的return语句执行之后还是之前?

    import java.util.HashMap; import java.util.Map; public class FinallyDemo1 { public static void main( ...

  7. C#中try catch中throw ex和throw方式抛出异常有何不同_异常捕获堆栈丢失问题

    前言,最近遇到一个使用try-catch异常捕获后记录一下日志,然后再抛出该异常后,异常堆栈里无法显示准确的堆栈地址的问题?   其实以前也遇到过类似问题,没有重视,这次好好研究了下,并上度娘上找了找 ...

  8. spring事务——try{...}catch{...}中事务不回滚的几种处理方式(转载)

    转载自   spring事务——try{...}catch{...}中事务不回滚的几种处理方式   当希望在某个方法中添加事务时,我们常常在方法头上添加@Transactional注解 @Respon ...

  9. java-try catch中return在finally之前 还是之后执行

    finally语句在return语句执行之后return返回之前执行的. finally块中的return语句会覆盖try块中的return返回. 如果finally语句中没有return语句覆盖返回 ...

随机推荐

  1. Asp.Net Core 程序部署到Linux(centos)生产环境(一):普通部署

    运行环境 照例,先亮底 centos:7.2 cpu:1核 2G内存 1M带宽 辅助工具:xshell xftp 搭建.net core运行环境 .net core 的运行环境我单独写了一篇,请看我的 ...

  2. ELK-logstash案例实战之读取日志输出到elasticsearch

    简介:从日志文件中读取日志,输出到elasticsearch集群中 $ cd /home/es/logstash-/config $ vim test3_es.conf $ cd /home/es/l ...

  3. 如何使用kafka增加topic的备份数量,让业务更上一层楼

    本文由云+社区发表 一.困难点 建立topic的时候,可以通过指定参数 --replication-factor 设置备份数量.但是,一旦完成建立topic,则无法通过kafka-topic.sh 或 ...

  4. AppBoxFuture(四). 随需而变-Online Schema Change

      需求变更是信息化过程中的家常便饭,而在变更过程中如何尽可能小的影响在线业务是比较头疼的事情.举个车联网监控的例子:原终端设备上传车辆的经纬度数据,新的终端设备支持同时上传速度数据,而旧的车辆状态表 ...

  5. C#杂记-自动实现的属性(自动属性)

    基础知识: 普通属性:可读或可写并将值存储到一个私有变量中的属性,不对数据做任何加工,没有自定义代码. private string name public string Name { get{ret ...

  6. Python 面向对象之反射

    Python 面向对象之反射 TOC 什么是反射? hasattr getattr setattr delattr 哪些对象可以使用反射 反射的好处 例子一 例子二 什么是反射? 程序可以访问.检查和 ...

  7. Dynamics 365检查工作流、SDK插件步骤是否选中运行成功后自动删除系统作业记录

    本人微信公众号:微软动态CRM专家罗勇 ,回复298或者20190120可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 系统 ...

  8. 自定义switchButton

    这篇博客要讲的是自定义switchButton,不过没有设置动画效果. 我用GradientDrawable来绘制switchButton,我们先看看最终的效果: 点击前: 点击后 接下来我们看看如何 ...

  9. C++ 虹软人脸识别 ArcFace 2.0 Demo

    环境配置: 开发环境:Win10 + VS 2013 SDK版本:ArcFace v2.0 OpenCV版本:2.4.9 平台配置: x64.x86下Release.Debug SDK 下载地址:戳这 ...

  10. 微信小程序(五) 利用模板动态加载数据

    利用模板动态加载数据,其实是对上一节静态数据替换成动态数据: