Skip to main content

Default exception handling in java



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

Popular posts from this blog

Core Java

Content 1 - Language fundamental   Identifier Reserved words Data types Literals Arrays Types of variables Var arg method Main method Java coding standards 2 -  Declaration and Access Modifiers Java source file structure Class modifiers Member modifiers Interfaces 3 - Exception Handling Introduction Runtime stack mechanism Default exception handling in java Exception hierarchy Customized exception handling by try-catch  Control flow in try-catch Methods to print exception information Try with multiple catch blocks Finally Difference between final, finally, finalize Control flow in try-catch-finally Control flow in nested try-catch-finally Various possible combinations of try-catch-finally throw keyword throws keyword Exception handling keywords summary Various possible compile-time errors in exception handling Customized exceptions Top-10 exceptions  4 - String Concept of String Immutable String String Comparison String Conca