Abhishek Aggarwal Abhishek Aggarwal - 1 year ago 89
Java Question

Why JVM terminates When main method in java ducks the exception of calling method?

we know that there is two law to handle exception . First one is to catch the exception by using try/catch block and other one is ducks the exception and pass it to the calling method . When main method do that i.e ducks the exception then JVM terminates . I want to know why it happens .

class Moon
static void test() throws Exception
throw new Exception();

public static void main(String[] args) throws Exception

Answer Source

Short answer : Exception is caused and never handled.

Detailed answer : A execution path starts from 'main' method. Main method calls a code which which throws an exception, this returns the control to the place in code which invoked the exception causing code. The caller code does not take care of the exception and rather decides to throw it to the code which caused its invocation. This is the JVM code. Now JVM also does not take care of the exception (it should never consume any exception, as they are part of user code logic). Hench JVM decides to simply terminate.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download