今天碰到了这样一个问题;使用try,return和finally会碰到的各种情况
1,try中有return时,执行顺序;
2,try和finally中都有return时,执行顺序;
3,运算代码在finally中,try中return返回值时的执行顺序;
代码:
public class tryfinally {
static int i=0;
public static void main(String[] args) {
  int value=getValue1();
   System.out.println(value);
  System.out.println(getValue());
  System.out.println(getValue1());
  System.out.println(getValue2());
  System.out.println(getValue3());
}
@SuppressWarnings("finally")
public static boolean getValue(){
  try{
   return true;
  }finally{
   return false;
  }
}

@SuppressWarnings("finally")
public static int getValue1(){
  try{
   return i;
  }finally{
   i++;
   System.out.println(i);
  }
}

@SuppressWarnings("finally")
public static int getValue2(){
  int y=0;
  try{
   return y;
  }finally{
   y++;
   System.out.println(y);
  }
}
}

@SuppressWarnings("finally")
public static int getValue3(){
                int z=0;
  try{
   return z;
  }finally{
   z++;
  }
}
结果:false
1
0
1
0
1
0
0

我的结论:代码按顺序执行,有多个return时返回的同一个值时后者覆盖前者。

最新文章

  1. for循环与for in,$('').each 与$.each的区别
  2. 浅谈时钟的生成(js手写代码)
  3. LINQ系列:Linq to Object串联操作符
  4. sybase 收集常用sql语句
  5. [转载]C/C++可变参数之va_start和va_end使用详解
  6. 学习笔记:HSB、HSL
  7. 如何在JBoss WildFly 8 自定义log4j日志
  8. 简单几步让Chrome浏览器也能打开Oracle EBS
  9. redhad借用CentOs yum 安装
  10. IOS设置背景色设置最简单方法
  11. #Leet Code# Gray Code
  12. 【转】MFC中调试过程中查看输出信息 -- 不错
  13. XJOI网上同步训练DAY5 T1
  14. Python的TkinterButton做为父窗口
  15. 一个不可思议的MySQL慢查分析与解决
  16. Alpha冲刺Day11
  17. [LeetCode] Split Array with Equal Sum 分割数组成和相同的子数组
  18. phpstorm 不能自动打开上次的历史文件
  19. 06 自学Aruba之win7系统802.1x认证网卡设置指导
  20. MYSQL和oracle 大小写问题

热门文章

  1. Elastic Stack 笔记(十)Elasticsearch5.6 For Hadoop
  2. pip安装路径
  3. 09 (OC)* 键路径(keyPath)、键值编码(KVC)、键值观察(KVO)
  4. java反射机制,以及对反射机制的了解
  5. ASP.NET Core 3.0 : 二十五. TagHelper
  6. 5分钟了解Prometheus
  7. Postman 之 HTTP Multipart/form-data 调试
  8. Spring boot 官网学习笔记 - Using Spring Boot without the Parent POM,但是还要使用Parent POM提供的便利
  9. 通过父级id获取到其下所有子级(无穷级)——Mysql函数实现
  10. springboot结合mybatis使用pageHelper插件进行分页查询