try/catch block return with finally clause in java(java中带有finally子句的try/catch块返回)
问题描述
鉴于以下 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 中执行吗?" 我可以看到程序的输出将是 'This is the finally 块'.但是,我不知道这是怎么可能的,因为 print 语句前面有一个 return...
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.
推荐答案
finally
在 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语言规范明确提到finally在不同条件下的执行:
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
这篇关于java中带有finally子句的try/catch块返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java中带有finally子句的try/catch块返回


- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01