大家好,今天我们来讲一个笔试和面试偶尔都会问到的问题,并且在工作中不知道原理,也会造成滥用。

大家可能都知道,try 块用来捕获异常,catch块是处理try块捕获的异常,finally 块是用来关闭资源。一个try块后面可以跟多个catch块,如果后面一个catch块也不跟,就一定要跟一个finally 块。

结论1:当在try块遇到return语句时,finally语句块将在方法返回之前被执行,但是返回值不受finally块中重新赋值的影响。

public class FinallyTest {
public static void main(String[] args) throws Exception {
int a = 10;
int sum = throwException(a);
System.out.println("执行返回结果sum:" + sum);
} public static int throwException(int a) throws Exception{
int b = 20;
int result = 0;
try{
System.out.println("执行try语句块");
result = a+b;
return result;
}catch(Exception e){
System.out.println("执行catch语句块");
}finally{
System.out.println("执行finally语句块");
result = 1;
}

return result;

    }
}

结论2:当在catch块遇到return或者throw异常语句时,finally语句块将在方法返回之前被执行,但是返回值不受finally块中重新赋值的影响。

public class FinallyTest {
public static void main(String[] args) throws Exception {
int a = 10;
int sum = throwException(a);
System.out.println("执行返回结果sum:" + sum);
} public static int throwException(int a) throws Exception{
int b = 20;
int result = 0;
try{
System.out.println("执行try语句块");
result = b / 0;
return result;
}catch(Exception e){
System.out.println("执行catch语句块");
return result;
}finally{
System.out.println("执行finally语句块");
result = 1;
} }
}

结论3:如果try,finally语句里均有return,忽略try的return值,而使用finally的return值

public class FinallyTest {
public static void main(String[] args) throws Exception {
int a = 10;
int sum = throwException(a);
System.out.println("执行返回结果sum:" + sum);
} public static int throwException(int a) throws Exception{
int b = 20;
int result = 0;
try{
System.out.println("执行try语句块");
result = a + b;
return result;
}catch(Exception e){
System.out.println("执行catch语句块");
}finally{
System.out.println("执行finally语句块");
result = 1; return result;
} }
}

结论4 :在finally语句块发生了异常,finanly语句块中异常后面的代码不会再执行。

public class FinallyTest {
public static void main(String[] args) throws Exception {
int a = 10;
int sum = throwException(a);
System.out.println("执行返回结果sum:" + sum);
} public static int throwException(int a) throws Exception{
int result = 30;
try{
System.out.println("执行try语句块");
return result;
}catch(Exception e){
System.out.println("执行catch语句块");
}finally{
int exception = b / 0;
System.out.println("执行finally语句块");
result = 1;
}
return result;
}
}

结论5:try语句块发生异常,并且finally语句块也发生了异常,finally块中的异常会掩盖try块中的异常。

public class FinallyTest {
public static void main(String[] args) throws Exception {
int a = 10;
int sum = throwException(a);
System.out.println("执行返回结果sum:" + sum);
} public static int throwException(int a) throws Exception{
int result = 0;
try{
System.out.println("执行try语句块");
result = a / 0 ;
return result;
}catch(Exception e){
throw new Exception(e);
}finally{
int[] arr = new int[1];
arr[2] = 3;
System.out.println("执行finally语句块");
}
}
}

  

通过上面的5个案例,我们应该对try 块,catch块,finally 块中return的执行顺序有了清晰的理解,下面我们在进行总结一下。

1. 当在try块遇到return语句时,finally语句块将在方法返回之前被执行,但是返回值不受finally块中重新赋值的影响。

2. 当在catch块遇到return或者throw异常语句时,finally语句块将在方法返回之前被执行,但是返回值不受finally块中重新赋值的影响。

3. 如果try,finally语句里均有return,忽略try的return,而使用finally的return。

4 . 在finally语句块发生了异常,finanly语句块异常后面的代码不会在执行。

5:try{}语句块发生异常,并且finally语句块也发生了异常,finally块中的异常会掩盖try块中的异常。

另外,关于1,2两点,不受finally块影响的原理,我做一个补充,为什么在finally块改变try块中的返回值,结果不受影响了?

如果try语句里有return,返回的是try语句块中的局部变量值。 详细执行过程如下:

①首先我们在try快中,会把返回值保存到局部变量中;

② 然后执行jsr指令跳到finally语句里执行;

③ 执行完finally语句后,返回之前保存在局部变量表里的值,所以finally块里改变try块中的返回值,不会生效。(但是上面第3点场景,finally自己带return返回值除外)

感谢大家的观看,如有错误之处,欢迎指出,共同学习,共同进步~

如果大家看我的文章觉得有所收获,请将文章分享给你们的朋友,后续我会更新更多浅显,优质的技术文章!

 

最新文章

  1. 高效 Java Web 开发框架 JessMA v3.4.1
  2. Ubuntu安装Svn,提供http访问
  3. Browsersync — 省时的浏览器同步测试工具
  4. If WCF Service side and Client side config is different?!
  5. openfire升级指南
  6. Java 8 VM GC Tuning Guide Charter3-4
  7. cnUVA情况
  8. android host
  9. android 中使用缓存加载数据
  10. TCP粘包拆包问题
  11. TOGAF架构内容框架之内容元模型(下)
  12. 转:selenium 并行启动多个浏览器
  13. webpack之前端性能优化(史上最全,不断更新中。。。)
  14. HTML简单使用
  15. ubuntu宽带连接
  16. spring自带测试配置
  17. HTML 介绍及标签
  18. PyQt5系列教程(六)如何让界面和逻辑分离
  19. 如何解决XMLHttpRequest cannot load file:~~~~~~~~~~~. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-res
  20. 【协议】3、HTTP 协议入门

热门文章

  1. UE4中如何使物体始终朝向摄像头?
  2. Java Collections类
  3. Django入门五之admin管理
  4. Python Cook函数笔记 【第一章】
  5. IAAS-libvirt介绍。
  6. python_自定日历
  7. es6属性基础教学,30分钟包会
  8. C# 使用 SmtpClient.SendAsync 方法发送邮件失败,总是返回 Cancelled
  9. struct和union的区别
  10. linux几种时间函数总结