在Java中,是“最后”块保证被调用(在主方法中)? [英] In Java, is the "finally" block guaranteed to be called (in the main method)?

查看:123
本文介绍了在Java中,是“最后”块保证被调用(在主方法中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Java菜鸟,我想知道,如果我有以下典型的Java代码

  public class MyApp { 
public static void main(String [] args){
try {
// do stuff
} catch {
// handle errors
} finally {
//清理连接等
}
}
}

JVM是否保证finally块始终运行?要了解我来自哪里,我习惯于C / C ++程序,如果您取消引用NULL指针,则可能会崩溃,并且您无法在之后运行任何代码。



但是,正如我理解Java和整个GC /托管内存业务一样,没有像空指针解引用这样的东西,一切都是可以预见的,所以我的程序没有办法崩溃那可以让它跳过最后,还是在那里?例如,在Python中,我通常会执行

  try:
#do stuff
除了AnExceptionIKnewMightHappen:
#以适当的方式做出反应
除了:
#log这个奇怪的错误我不知道可能会发生

,我没有通过我的代码,没有任何应用程序死亡。



当然,如果操作系统由于某种原因杀死了进程(或者如果某些东西杀死了整个系统,就像拔出插头)Java没有太多可以做的事情。另外,从PHP我知道你不能保护的不可捕获的错误,即使解释器在发生后仍然存在(至少可以输出正确的消息)。



编辑:为了清楚起见(没有被任何人误解),让我补充说,我正在寻找我的代码 导致终于被绕过。所以指向System.exit是一个有用的提醒,即使我不明白为什么我想做这样的事情。



JVM退出是一个相当明显的我认为这是一个外部事业。该笔记指出,当JVM和应用程序继续运行时,您也必须记住退出的线程的可能性,因为即使现在对我来说似乎也很明显,我没有想到。

解决方案

基本上是,除了注释列在这里(强调我的):


如果JVM退出尝试或捕获代码正在执行,
然后finally块可能不执行。同样,如果执行try或catch代码的线程
被中断或者被杀死,即使整个应用程序
继续,最终的
也可能不执行



I'm a Java rookie and I was wondering, if I have the following typical Java code

public class MyApp {
  public static void main(String[] args) {
    try {
      // do stuff
    } catch {
      // handle errors
    } finally {
      // clean up connections etc.
    }
  }
}

does the JVM guarantee that the finally block will always be run? To understand where I'm coming from, I'm used to C/C++ programs that might just crash if you dereference a NULL pointer and you can't have any code to be run after that.

But as I understand Java and the whole GC / managed memory business in general, there's no such thing as a null pointer dereferencing, everything is a catchable expection, so there's not really a way for my program to crash that could make it skip the finally, or is there? For example, in Python, I usually do

try:
  # do stuff
except AnExceptionIKnewMightHappen:
  # react in an appropriate way
except:
  # log that weird error I had not known could happen

and I have never had any app die without passing through my code.

Of course, if the OS for some reason kills the process (or if something kills the whole system, like pulling the plug) there's not much Java can do. Also, from PHP I know non-catchable errors that you can't protect against, even though the interpreter was still there after it happened (at least it is able to output a proper message).

Edit: Just for clarity (it wasn't really misunderstood by anyone), let me add that I was looking for things inside my code that could lead to the finally being bypassed. So pointing to System.exit was a helpful reminder, even though I can't see why I would want to do something like that.

The JVM exiting is a rather obvious way and I'd count that as an external cause. The note pointing out that you also have to remember the possibilty of threads exiting while the JVM and the app keep running was very helpful, because even though it also seems obvious to me now, I hadn't thought of it.

解决方案

Basically yes, except for the note listed here (emphasis mine):

If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.

这篇关于在Java中,是“最后”块保证被调用(在主方法中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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