Harsha
Harsha

Reputation: 3578

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?

Upvotes: 0

Views: 81

Answers (2)

AlexR
AlexR

Reputation: 115328

There is a global exception handling in JVM layer.

EDIT.

You can implement global exception handling using Thread.setDefaultUncaughtExceptionHandler()

Upvotes: 3

Akash Thakare
Akash Thakare

Reputation: 22972

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

Upvotes: 1

Related Questions