简体   繁体   中英

Why does the program not terminate when a method that throws RuntimeException is called without handling the exception in Java?

Class 1:

package hf;

public class apples
{
    public static void main(String[] args)
    {
        Good good = new Good();

        good.method();
    }
}

Class 2:

package hf;

public class Goodie extends NullPointerException
{

}

Class 3:

package hf;

public class Good 
{
    public void method() throws Goodie
    {
        System.out.println("Goodmethod");
        throw new Goodie();
    }
}

I have not used any try/catch block to catch the NullPointerException(which extends RunTimeException) that is thrown when I call good.method() in class 1. When I run the program, the console in Eclipse indicates that the program is still running because it does not show at the top of the console box, like it usually does when the program execution is over.

Why is the program still running?

How can I bring the program execution to halt without pressing the red stop button?

The program you have posted will terminate when you throw the uncaught exception. I just tested it myself.

The only way to prevent the JVM from terminating is to have non-daemon threads running. If you have displayed a GUI for instance, you must make sure you terminate the EDT to for the application to terminate completely.

当我们在Eclipse中以调试模式运行应用程序时,如果设置了Windows->Preferences->Java->Debug->Suspend execution on uncaught exceptions ,它将在未捕获的异常上停止。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM