简体   繁体   English

Java如何在没有try-catch的情况下显示异常

[英]How does java show exceptions without try-catch

Java can show exceptions in the terminal although the error prone codes between try-catch block is not enclosed. Java可以在终端中显示异常,尽管没有包含try-catch块之间易于出错的代码。 How does Java do that? Java如何做到这一点?

There is a global exception handling in JVM layer. JVM层中有一个全局异常处理。

EDIT. 编辑。

You can implement global exception handling using Thread.setDefaultUncaughtExceptionHandler() 您可以使用Thread.setDefaultUncaughtExceptionHandler()实现全局异常处理

According to the Java API documentation, when a thread is going to terminate due to an uncaught exception, the Java Virtual Machine will query the thread for its UncaughtExceptionHandler 根据Java API文档,当线程由于未捕获的异常而终止时,Java虚拟机在线程中查询UncaughtExceptionHandler

As our program has main Thread JVM ask main about Exception Handler by Thread.getUncaughtExceptionHandler() . 由于我们的程序具有主线程JVM,请通过Thread.getUncaughtExceptionHandler()向主询问有关异常处理程序的Thread.getUncaughtExceptionHandler()

Than invokes handler by passing Thread and Exception as an argument to handle Exception 比通过传递Thread和Exception作为处理Exception的参数来调用处理程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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