前言
由于总是搞不清楚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. JVM(四)垃圾回收的实现算法和执行细节

    全文共 1890 个字,读完大约需要 6 分钟. 上一篇我们讲了垃圾标记的一些实现细节和经典算法,而本文将系统的讲解一下垃圾回收的经典算法,和Hotspot虚拟机执行垃圾回收的一些实现细节,比如安全点 ...

  2. 痞子衡嵌入式:飞思卡尔i.MX RT系列MCU启动那些事(8)- 从Raw NAND启动

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是飞思卡尔i.MX RT系列MCU的Raw NAND启动. 前面铺垫了七篇启动系列文章,终于该讲具体Boot Device了,我们知道i. ...

  3. javascript基础修炼(1)——一道十面埋伏的原型链面试题

    在基础面前,一切技巧都是浮云. 题目是这样的 要求写出控制台的输出. function Parent() { this.a = 1; this.b = [1, 2, this.a]; this.c = ...

  4. 第49章 令牌端点(Token Endpoint) - Identity Server 4 中文文档(v1.0.0)

    令牌端点可用于以编程方式请求令牌.它支持password,authorization_code,client_credentials,refresh_token和urn:ietf:params:oau ...

  5. 第37章 资源所有者密码验证(Resource Owner Password Validation) - Identity Server 4 中文文档(v1.0.0)

    如果要使用OAuth 2.0资源所有者密码凭据授权(aka password),则需要实现并注册IResourceOwnerPasswordValidator接口: public interface ...

  6. [Go] golang结构体成员与函数类型

    package main import ( "fmt" ) //定义一个类型 type tsh struct { //定义成员,类型是func() string test func ...

  7. vuex最详细完整的使用用法

    来自:https://blog.csdn.net/qq_35430000/article/details/79412664#commentBox  github仓库地址:https://github. ...

  8. 【20190228】JavaScript-数组的操作

    1. 创建数组 var array = new Array(); var array = new Array(5); var array = new Array(1,2,3,"a" ...

  9. Registrator中文文档

    目录 快速入门 概述 准备 运行Registrator 运行Redis 下一步 运行参考 运行Registrator Docker选项 Registrator选项 Consul ACL令牌 注册URI ...

  10. 新版的nuget包 PackageLicense 这样写

    Intro 最近编译类库项目的时候发现总是有个 licenseUrl 的警告,警告信息如下: warning NU5125: The 'licenseUrl' element will be depr ...