java异常会终止整个java应用程序吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18607912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:28:44  来源:igfitidea点击:

Will a java exception terminate the whole java application?

javamultithreadingexceptionexception-handlingjvm

提问by wuchang

I used to think that when an exception happened, the whole java application will be terminated. For example, I write a test function to test my idea.

我曾经认为当发生异常时,整个java应用程序都会被终止。例如,我编写了一个测试函数来测试我的想法。

public void test(){
    File fileDir=new File(sourceDataDir);
    if(fileDir.exists()){
        File[] files = fileDir.listFiles();
        for(int index=0 ; index<files.length ; index++){
            System.out.println("index = "+index);
            File file = files[index];
            if(index == 1)//delete a file to cause a FileNotFoundException
                file.delete();
            try {
                BufferedReader in = new BufferedReader(new FileReader(file));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}

I delete a file to cause a FileNotFoundExceptionmanually. I used to think that the whole application will terminate when the exception happened. But in fact, the application will continue reading the remaining files. So, my question is, in what condition an exception will cause the whole application to be terminated?

我删除了一个文件导致FileNotFoundException手动。我曾经认为整个应用程序会在异常发生时终止。但实际上,应用程序会继续读取剩余的文件。所以,我的问题是,在什么情况下异常会导致整个应用程序终止?

回答by Peter Lawrey

my question is ,in what condition ,a exception will cause the whole application to be terminated?

我的问题是,在什么情况下,异常会导致整个应用程序终止?

It won't ever. Only System.exit() causes the whole program to terminate (and a JVM crash)

它永远不会。只有 System.exit() 导致整个程序终止(和 JVM 崩溃)

Think of an Exception and like a powerful break;which can break out of methods. A break could result in a loop exiting and if the last loop in the only non daemon thread, the program will exit. But it doesn't cause it, even though it might be the last thing you see before you program dies.

想想一个异常,就像一个强大的break;可以打破方法的人。中断可能导致循环退出,如果最后一个循环在唯一的非守护线程中,程序将退出。但它不会导致它,即使它可能是您在程序死亡之前看到的最后一件事。

回答by sr01853

No. Exception handlersof java handles the abnormal execution of the program

Exception handlers的java处理程序的执行异常

回答by David Lakatos

There are two main types of exceptions:

有两种主要类型的异常:

  • Not runtime exceptions
  • 不是运行时异常

It indicates the system is working properly. You must catch the exception or it won't compile. It will never terminatethe application. If you want to terminate anyway, you have to call System.exit(ERROR_NUMBER) in the catch block or throw a runtime exception.

它表明系统工作正常。您必须捕获异常,否则它将无法编译。它永远不会终止应用程序。如果您无论如何都想终止,则必须在 catch 块中调用 System.exit(ERROR_NUMBER) 或抛出运行时异常。

  • Runtime exceptions
  • 运行时异常

Indicates a system error (eg. application server misconfiguration). You don't have to catch it. If you don't catch it, it terminatesthe application or if you write a J2EE application the AS might handle it and continue your app.

表示系统错误(例如应用程序服务器配置错误)。你不必抓住它。如果您没有发现它,它会终止应用程序,或者如果您编写 J2EE 应用程序,AS 可能会处理它并继续您的应用程序。

回答by Aniket Thakur

So, my question is, in what condition, a exception will cause the whole application to be terminated?

所以,我的问题是,在什么情况下,异常会导致整个应用程序终止?

See docsfor details on Exceptions. As for your problem FileNotFoundException is a checked Exception which means you need to handle it. Now you have to options

有关异常的详细信息,请参阅文档。至于您的问题 FileNotFoundException 是一个已检查的异常,这意味着您需要处理它。现在你必须选择

  1. Catch the FileNotFoundException and handle(you can simply print stack trace and proceed)
  2. or you can throw it to the parent(calling method)
  1. 捕获 FileNotFoundException 并处理(您可以简单地打印堆栈跟踪并继续)
  2. 或者你可以把它扔给父级(调用方法)

In case 1 your java process will continue till it reaches the end(assuming no runtime Exceptions/errors occur).

在第 1 种情况下,您的 Java 进程将一直持续到结束(假设没有发生运行时异常/错误)。

In case 2 if you do not catch your FileNotFoundException even in the parent(calling function) and just throw it again and continue to do so, the exception will finally land up in the main() method. If even your main() method throws this exception JVM will simply shut down.

在第 2 种情况下,如果即使在父级(调用函数)中也没有捕获 FileNotFoundException 并且只是再次抛出它并继续这样做,则异常最终会出现在 main() 方法中。即使您的 main() 方法抛出此异常,JVM 也会简单地关闭。

Update for clarifying your comments:

更新以澄清您的评论:

Catching an Exception is independent of whether Excpetion is catched ot not. Inc ase of uncahed Exception you can still catch it and let the program proceed. But that is not recommended because by definition of uncached Exception(which should not happen at all) you are not suppose to recover if uncached Exception occurs.

捕获异常与是否捕获到异常无关。Inc ase of uncahed Exception 您仍然可以捕获它并让程序继续。但不推荐这样做,因为根据未缓存异常的定义(根本不应该发生),如果发生未缓存异常,您将不会恢复。

Consider following simple example

考虑以下简单示例

public static void main(String args[]) {
    try {
        String s = null;
        System.out.println(s.length());
    } catch (Exception e) {
        System.out.println("Catch runtime exception but not quite sure what to do with it");
    }

    System.out.println("Reached here even after uncatched Exception");
}

output is

输出是

Catch runtime exception but not quite sure what to do with it
Reached here even after uncatched Exception

So basically whenever Exception occurs if you do not catch it at any level from the point of origin, it will eventually propagate to main() and JVM will eventually shut down. If you do catch it(irrespective of catched or uncatched Exception) your program will proceed(output may not be as expected in case of uncatched Exceptions) and terminate.

因此,基本上每当发生异常时,如果您没有从源点的任何级别捕获它,它最终将传播到 main() 并且 JVM 最终将关闭。如果您确实捕获了它(无论是捕获的还是未捕获的异常),您的程序将继续运行(在未捕获的异常的情况下,输出可能不符合预期)并终止。

回答by Holger

If an exception is not catched, the thread in which the exception occurred will be terminated. If no non-daemon threads remain the JVM will terminate. That's the only way how an exception might terminate a JVM. If you catch an exception it will never cause a JVM termination.

如果没有捕获到异常,则将终止发生异常的线程。如果没有剩余的非守护线程,JVM 将终止。这是异常终止 JVM 的唯一方式。如果您捕获异常,它永远不会导致 JVM 终止。