前言
由于总是搞不清楚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快速入门】(一)环境安装

    下载.NET Core SDK 下载地址:https://www.microsoft.com/net/download/windows https://www.microsoft.com/net/le ...

  2. SpringBoot系列——Spring-Data-JPA(升级版)

    前言 在上篇博客中:SpringBoot系列——Spring-Data-JPA:https://www.cnblogs.com/huanzi-qch/p/9970545.html,我们实现了单表的基础 ...

  3. 一统江湖的大前端(4)shell.js——穿上马甲我照样认识你

    <一统江湖的大前端>系列是自己的前端学习笔记,旨在介绍javascript在非网页开发领域的应用案例和发现各类好玩的js库,不定期更新.如果你对前端的理解还是写写页面绑绑事件,那你真的是有 ...

  4. lua的String

    基础字符串函数 字符串库中有一些函数非常简单,如:    1). string.len(s) 返回字符串s的长度:    2). string.rep(s,n) 返回字符串s重复n次的结果:    3 ...

  5. Asp.Net MVC 读取json文件

    有些系统上面的配置可以做成config里面的appsetting.这里要求写在json文件里面. 首先 添加命名空间 using Newtonsoft.Json; using System.IO; u ...

  6. html的标签分类————可以上传的数据篇

    html的标签可以分为: 块级标签:div(白板),H系列(加大加粗,H1—H7,字体一般逐渐变小,一般用作标题),p标签(段落之间有间距) 行内标签:span(白板) 此外,标签之间是可以嵌套的.为 ...

  7. 从.Net到Java学习第十一篇——SpringBoot登录实现

    从.Net到Java学习系列目录 通过前面10篇文章的学习,相信我们对SpringBoot已经有了一些了解,那么如何来验证我们的学习成果呢?当然是通过做项目来证明啦!所以从这一篇开始我将会对之前自己做 ...

  8. BGP:所有邻居都启动了BGP,则无须建立首尾逻辑邻居,否则就需要首尾建立逻辑邻居。

    配置说明:都通过loopback 口作为bgp 连接口,并且要配置ebgp多跳,同时配置loopback口的静态路由. 以AR2为例: 第一种场景:所有直接相连的邻居都启动了BGP,则路由可以随意扩散 ...

  9. Android ION内存分配

    The Android ION memory allocator 英文原文 ION heaps ION设计的目标 为了避免内存碎片化,或者为一些有着特殊内存需求的硬件,比如GPUs.display c ...

  10. QT中pro文件编写的详细说明

    如果用QTCreator开发的小伙伴,可能都知道.pro文件,但是里面的具体配置可能比较模糊,今天我就来给大家好好讲解下 一.名称解释 QT += :这个是添加QT需要的模块 TARGET = :生成 ...