try / catch块返回java中的finally子句 [英] try/catch block return with finally clause in java

查看:130
本文介绍了try / catch块返回java中的finally子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中给出以下try / catch块:

Given the following try/catch block in java:

try{
  return;
}
catch(SomeException e){
  System.out.println(e);
}
finally{
  System.out.println("This is the finally block");
}

根据这篇文章:最终总是用Java执行?我可以看到程序的输出将是'这是最后的块'。但是,我无法弄清楚这是怎么可能的,因为print语句前面有一个返回...

and according to this post: "Does finally always execute in Java?" I can see that the program's output will be 'This is the finally block'. However, I can't figure out how that would be possible since the print statement is preceded by a return...

我怀疑这种行为与此有关线程,但我不确定。请赐教。谢谢。

I suspect that this behaviour has something to do with threading, however I am not certain. Please enlighten me. Thank you.

推荐答案

最后 return 声明。由于java规则 finally 将始终执行,除非在JVM崩溃或调用 System.exit()的情况下。

finally is executed before return statement. As java rule finally will always be executed except in case when JVM crashes or System.exit() is called.

Java语言规范明确提到最终在不同条件下的执行:

Java Language Specification clearly mentions the execution of finally in different conditions:

http://docs.oracle.com/javase/specs/jls /se7/html/jls-14.html#jls-14.20.2

这篇关于try / catch块返回java中的finally子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆