当引用RuntimeException的方法被调用而不处理Java中的异常时,为什么程序不会终止?

编程入门 行业动态 更新时间:2024-10-27 21:14:10
本文介绍了当引用RuntimeException的方法被调用而不处理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(); } }

我没有使用任何try / catch块来捕捉当我在类1中调用good.method()时抛出NullPointerException(它扩展了RunTimeException)。当我运行程序时,Eclipse中的控制台表示程序仍在运行,因为它不会显示在控制台顶部与程序执行结束时通常一样。

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.

为什么程序仍在运行?

如何使程序执行停止而不用按红色停止按钮?

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.

防止JVM终止的唯一方法是让非守护进程的线程运行。如果您已经显示了GUI,那么您必须确保终止EDT以使应用完全终止。

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.

更多推荐

当引用RuntimeException的方法被调用而不处理Java中的异常时,为什么程序不会终止?

本文发布于:2023-06-10 22:57:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/620876.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   异常   程序   方法   RuntimeException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!