- Inside a method if any exception occurs the method in which it is arised is responsible to create exception object by including the following information .
- name of exception
- description of exception
- location at which exception occurs(stack trace)
- After creating exception object method hand overs that object to the jvm.
- Jvm will check wether the method contains any exception handling code or not, if the method doesn’t contain the exception handling code then jvm terminates that method abnormally and removes corresponding entries from the stack.
- Then jvm identifies caller method and checks wether caller method contains any handling code or not,
- If the caller method doesn’t contain handling code then jvm terminates that caller method also abnormally and removes the corresponding entry from the stack
- This process will be continued until main method and if the main method also doesn’t contain the handling code then jvm terminates main method abnormally and removes corresponding entry from the stack.
- Then jvm handovers responsibility of exception handling to default exception handler, which is the part of jvm. Default exception handler prints exception information in the following formats and terminates program abnormally.
- Exception in thread “name” name of Exception : description
- Stack trace
Example :-
After compiling and running the output will be -
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.nt.controller.Test.doMoreStuff(Test.java:15)
at com.nt.controller.Test.doStuff(Test.java:11)
at com.nt.controller.Test.main(Test.java:7)
After compiling and running the output will be -
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.nt.controller.Test.doMoreStuff(Test.java:15)
at com.nt.controller.Test.doStuff(Test.java:11)
at com.nt.controller.Test.main(Test.java:7)
- As you can see that exception arised in doMoreStuff() method and it doesn't have any handling code so, jvm will terminates this method abnormally and removes from the stack.
- After that it will go to doStuff() method where doMoreStuff() was called, if doStuff() has also not handled this exception then it will also terminates abnormally and removes from the stack by the jvm.
- it will be continues till control goes to main method and if main() method also not handled this exception then it will also terminates abnormally and removes from the stack by the jvm and handovers responsibility of exception handling to default exception handler.
No comments:
Post a Comment