Explain Default exception handling in Java?
- Inside a method, if any exception occurs the method in which it is raised 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 handover that objects to the JVM.
- JVM will check whether the method contains any exception handling code or not.
- If the method doesn’t contain exception handling code than JVM terminates abnormally and removes corresponding entry from the stack.
- Then JVM identifies caller method and checks whether caller method contains any handling code or not.
- If the caller method doesn’t contain handling code then JVM terminates the caller method also abnormally and removes corresponding entry from the stack.
- This process will be continued until mail main method and if the main method also doesn’t contain handling code then JVM terminate the main method abnormally and removes corresponding entry from the stack.
- Then JVM handovers responsibility of exception handling to default exception handler, which the part of JVM.
- Default exception handler print exception info in the following format and terminate program abnormally.
Note:-Â In a program, at least one method terminates abnormally then the program termination is abnormal termination.Â If all method terminated normally then only program termination is normal termination.Â
BenefitsÂ of handling theÂ exceptions
- Separation ofÂ the error logic or exception logic from our regular business logic.
- Grouping andÂ differentiatingÂ the exception types.
- Handling the exception and making the program to terminate normally orÂ successfully.