简体   繁体   中英

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. How does Java do that?

There is a global exception handling in JVM layer.

EDIT.

You can implement global exception handling using 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

As our program has main Thread JVM ask main about Exception Handler by Thread.getUncaughtExceptionHandler() .

Than invokes handler by passing Thread and Exception as an argument to handle Exception

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