EXCEPTION HIERARCHY

Exception Hierarchy


    javapadho
    Exception Hierarchy
  • Throwable is a base class of all Abnormal conditions.

  • Exception is super class of all Exceptions.

  • Abnormal conditions which are raised at runtime are called as RuntimeExceptions.

  • All runtime Exception are called or come under unchecked type Exceptions.

  • Runtime Exception are not found at compile time.Why because runtime exceptions are unknown to compiler. So it won’t be raised at compile time it will come at the time of running.

  • Checked Exceptions are compile time exceptions and these exceptions are known by the compiler. Since it is found at the time of compilation. So to rectify these exceptions we should handle them by using try & catch (or) by using throws keyword.

  • Unchecked exception are exception which are not know by compiler .mean compiler not check at compile time (compile succefully), in unchecked exception occur at Runtime of program.

Example 1:

 
                
             package com.sdj;
             class A
             {
                int test()
                {
                   try
                   {
                      System.out.println("try");
                   }
                   catch (NumberFormatException ne) 
                   {
                      System.out.println("catch");
                   }
                   finally 
                   {
                      System.out.println("finally");
                   }
                   return 10;
                }
             }
             public class Manager
             {
                public static void main(String[] args) 
                {
                   A a1=newA();
                   a1.test();
                   System.out.println(a1.test());
                }
             }                

Output:

try finally try finally 10

Example 2:

 
                
             package com.sdj;
             class A
             {
                int test(String k)
                {
                   int i;
                   try
                   {
                      System.out.println("try");
                      i=Integer.parseInt(k);
                      return i;
                   }
                   catch (NumberFormatException ne) 
                   {
                      System.out.println("catch");
                   }
                   finally
                   {
                      System.out.println("finally");
                   }
                   return i;
                }
             }
             public class Manager 
             {
                public static void main(String[] args) 
                {
                   A a = new A();
                   System.out.println(a.test(args[0]));
                }
             }                
      If we run the above program like java Manager guna then the output is as below

    Output:

         try —→   it is o/p from try block .
         catch —→   it is o/p from catch block with NFE . why because we are passing a string type cmdLineargmt as guna but we are converting String to Integer value. So we will get NumberFormatException .i.eparseInt() will raise the exception if input String ‘guna’ isn’t a numeric value.
         finally —→   it is o/p from finally block.
         30 —→   it is o/p from return value at line number 21.

      If we run the above program like java Manager 50 then output is as follows

    Output:

         try —→   it is o/p from try block.
         catch —→   it is o/p from finally block.
         50 —→   it is o/p from return value at line number 11. Why because we are passing a cmdLine argument as 50 . it will come to try block & it will convert String to integer and return the integer value at line number 11. Then it skips from that statement.

Example 3:

 
                
             package com.sdj;
             class A
             {
                int test(String k)
                {
                   int i;
                   try 
                   {
                      System.out.println("try");
                      i=Integer.parseInt(k);
                      return i/i-9;
                   }
                   catch (NumberFormatException ne) 
                   {
                      System.out.println("catch");
                      return 22;
                   }
                   catch (ArithmeticException ae)
                   {
                      System.out.println("catch ae");
                      return 20;
                   }
                   return 30;
                }
             }
             public class Manager 
             {
                public static void main(String[] args) 
                {
                   A a1=new A();
                   System.out.println(a1.test(args[0]));
                }
             }                
  If we keep return statements in try & catch blocks then execution is as folows

(A). in the above program the return statement in try block & in all catch block

  • Normal execution with the return statement in try block.

  • Normal execution with NFE and return statement in catch block-1.

  • Normal execution with AE and return statement in catch block-2.

(B). in the above program there is no return statement in try block

  • Normal execution with the return statement is end of method.(if there is no return in try block)

  • Normal execution with NFE and return statement in catch block-1.

  • Normal execution with AE and return statement in catch block-2.

(C). in the above program there is no return statement in AE catch block

  • Normal execution with the return statement in try block.

  • Normal execution with NFE and return statement in catch block-1.

  • Normal execution with AE and return statement in end of the method.

(D). in the above program there is no return statement in NFE catch block

  • Normal execution with the return statement in try block.

  • Normal execution with NFE and return statement in end of the method.

  • Normal execution with AE and return statement in catch block-2.


If keep return statements like bellow

javapadho
return statements with try, catch, finally

Example 4:

 
                
             try 
             {
                
             }
             catch (ArithmeticException ae)
             {
                System.out.println("arithmetic type");
             }                

Explanation :

The above program will make compilation success why because unchecked type exception in try block may/may not contain corresponding code to produce an exception. Then the catch block can handle the corresponding runtime exception.

Example 5:

 
                
             try 
             {
                
             }
             catch (ClassNotFoundExceptionc ae)
             {
                System.out.println("class Not found");
             }                

Explanation :

  • At line number 5 compilation fails why because we are keeping catch block with checked type exception.

  • If we keep any catch block with checked type Exception then try block should contain the code for produce atleast chance to raise that corresponding checked type Exception then only compile will success else compilation will fail.

  • Inside try block we should have code to raise a corresponding Exception else at compile time error will occured.

  • Why because checked type Exceptions are known to compiler but incase of unchecked type exception try block may/may not contain corresponding code to produce unchecked Exception then also compile is success.

Example 6:

 
                
             try 
             {
                int i=20/0;   
             }
             catch (ClassNotFoundExceptionce)
             {
                System.out.println("class type");
             }                

Explanation :

  • At line number 5 compilation fails why because Unreachable catch block for ClassNotFoundException. This exception is never thrown from the try statement body.

  • try statement body will raise the unchecked exception i.e. ArithmeticException. But catch block will handle the checked type exception.

Example 7:

 
                
             try 
             {
                Class.forName("test.txt");
             }
             catch (FileNotFoundExceptionfe)
             {
                System.out.println("class type");
             }                

Explanation :

  • At line numbers 3&5 compilation fails why because at line number 3 in try statement block it raises the ClassNotFoundException. But we are handling FileNotFoundException in catch block. There is no relationship between FileNotFoundException&ClassNotFoundException.

Note

In above program there is two chances to fail

  • At line number 3 Raised ClassNotFoundException. But we can’t handle.

  • At line number 5 handle FileNotFoundException. But no chances to raise that exception in try block.

Example 8:

 
                
             public class ExceptionDemo
             {
                public static void main(String[] args) 
                {
                   try 
                   {
                      String str=args[0];
                      int i=Integer.parseInt(str);
                      int reuslt=i/(i-9);
                   }
                   catch (RuntimeException re)
                   {
                      System.out.println("at runtime");
                   }
                   catch (ArithmeticException  ae)
                   {
                      System.out.println("at AE");
                   }
                   catch (ArrayIndexOutOfBoundsExceptionaie)
                   {
                      System.out.println("at AIEF");
                   }
                   catch (NumberFormatException  ne)
                   {
                      System.out.println("at NFE");
                   }
                }
             }                

Explanation :

  • At line number 17,21,25 compilation fails why because at try block 3 exceptions will be raised i.e.AIEF,NFE,AE. But we can handle those exceptions at line number 13 in catch block by using RuntimeException.

  • Here we are using generic type of that exceptions i.e. RuntimeException .It will handle all run time exceptions. That’s why it doesn’t go to specific exceptions.

Note
  • It is advisable to use (or)goes to specific to generic type of exceptions.

  • If we make the success above program then we should go for specific to generic type Exceptions only. It is advisable to use.

Example 9:

 
                
             package com.sdj;
             import java.io.FileNotFoundException;
             public class ExceptionDemo
             {
                public static void main(String[] args) 
                {
                   try
                   {
                      String str=args[0];
                      int i=Integer.parseInt(str);
                      int reuslt=i/(i-9);
                   }
                   catch (ArrayIndexOutOfBoundsExceptionaie)
                   {
                      System.out.println("at AIEF");
                   }
                   catch (NumberFormatException  ne)
                   {
                      System.out.println("at NFE");
                      catch (ArithmeticException  ae)
                      {
                         System.out.println("at AE");
                         
                         catch (RuntimeException re)
                         {
                            System.out.println("at runtime");
                         }
                      }
                   }
                }
             }                
  if we run the above program like java ExceptionDemo

Output:

     —→   at AIEF o/p from AIEF catch block.