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

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

问题描述

我是 Java 新手,我想知道我是否有以下典型的 Java 代码

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.
    }
  }
}

JVM 是否保证 finally 块会一直运行?为了理解我从哪里来,我已经习惯了 C/C++ 程序,如果您取消引用一个 NULL 指针,那么这些程序可能会崩溃,之后就无法运行任何代码.

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.

但据我了解 Java 和整个 GC/托管内存业务,没有空指针解引用这样的东西,一切都是可捕捉的预期,所以我的程序实际上没有办法崩溃跳过最后,还是有?比如在Python中,我通常会做

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.

当然,如果操作系统出于某种原因杀死了进程(或者如果某些事情杀死了整个系统,比如拔掉插头),Java 无能为力.此外,从 PHP 中我知道您无法防止的不可捕获的错误,即使解释器在它发生后仍然存在(至少它能够输出正确的消息).

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).

为了清楚起见(没有人真正误解),让我补充一点,我正在寻找在我的代码中可能导致终于被绕过了.所以指向 System.exit 是一个有用的提醒,尽管我不明白为什么我会想要做这样的事情.

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.

JVM 退出是一种相当明显的方式,我将其视为外部原因.指出您还必须记住当 JVM 和应用程序继续运行时线程退出的可能性非常有帮助,因为即使现在对我来说这也很明显,但我没有想到.

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):

如果在执行 try 或 catch 代码时 JVM 退出,那么finally块可能不会执行.同样,如果线程执行 try 或 catch 代码被中断或终止,finally块可能不会执行,即使应用程序作为一个整体继续.

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 中,是“finally"块保证被调用(在主方法中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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