Default exception handling in java
1) If an exception raised inside any method then the method is responsible to create an Exception object with the following information.
1) Name of the exception.
2) Description of the exception.
3) Location of the exception.
2) After creating that Exception object the method handovers that object to the JVM.
3) JVM checks whether the method contains any exception handling code or not. If the method won’t contain any handling code then JVM terminates that method abnormally and removes corresponding entry form the stack.
4) JVM identifies the caller method and checks whether the caller method contains any handling code or not. If the caller method also does not contain handling code then JVM terminates that caller also abnormally and the removes corresponding entry from the stack.
5) This process will be continued until the main() method and if the main() method also doesn’t contain any exception handling code then JVM terminates main() method and removes the corresponding entry from the stack.
6) Then JVM handovers the responsibility of exception handling to the default exception handler.
7) The default exception handler just prints exception information to the console in the following formats and terminates the program abnormally. Name of exception: description Location of exception (stack trace)
Example:
class Test
{
public static void main(String[] args)
{
doStuff();
}
public static void doStuff()
{
doMoreStuff();
}
public static void doMoreStuff()
{
System.out.println(10/0);
}
}
Output:
Runtime error
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.doMoreStuff(Test.java:10)
at Test.doStuff(Test.java:7)
at Test.main(Test.java:4)
Diagram:
Comments
Post a Comment