Presented By,
Vinod Kumar V H
Types of Exception

      Checked Exception
      Unchecked Exception
Common scenarios of Exception Handling
 ArithmeticException


       int a=50/0;

 NullPointerException


       String s=null;
       System.out.println(s.length());
Common scenarios of Exception Handling
 NumberFormatException


        String s=“abc”;
        int i=Integer.parseInt(s);

 ArrayIndexOutOfBoundsException


        int a[]= new int[5];
        a[10]=50;
Keywords used Exception Handling
            try
            catch
            finally
            throw
            throws
try
  class sample
  {
    public static void main(String args[])
    {
         int data=50/0;
         System.out.println(“I love india”);
    }
  }
try
  class sample{
     public static void main(String args[]){
          try{
                   int data=50/0;
           }
          catch(ArithmeticException e)
          {
                   System.out.println(e);
          }
          System.out.println(“I love india”);
     }
  }
Multiple catch
class sample{
  public static void main(String args[]){
       try{
              int a[]=new int[5];
              int a[5]=30/0; }
catch(ArithmeticException e){s.o.p(“task 1”);}
catch(ArrayIndexOutOfBoundsExceptin e){s.o.p(“task 2”);}
catch(Exception e){s.o.p(“task completed”);}
System.out.println(“I love india”);
  }
}
Multiple catch
class sample{
  public static void main(String args[]){
        try{
               int a[]=new int[5];
               int a[5]=30/0; }
catch(Exception e){s.o.p(“task completed”);}
catch(ArithmeticException e){s.o.p(“task 1”);}
catch(ArrayIndexOutOfBoundsExceptin e){s.o.p(“task 2”);}
System.out.println(“I love india”);
  }
}
Nested try
class sample{
   public static void main(String args[]){
        try{
            try{
                 s.o.p(“divide”);
                 int a=40/0;
        }catch(ArithmeticExceptin e){s.o.p(e);}
        try{
                 int a[]=new int[5];
                 int a[5]=4;
        }catch(ArrayIndexOutOfBoundsExceptin e){
                 s.o.p(e);}
        s.o.p(“other statements”);
   }catch(Exception e){s.o.p(“exception handled”);}
s.o.p(“normal flow”);
   }
}
finally
 Exception doesn’t occur
  class sample{
  public static void main(String args[]){
       try{
               int data=25/5;
               s.o.p(data);}
catch(NullPointerException e){s.o.p(e);}
finally{s.o.p(“finally block is always excuted”);}
s.o.p(“I love india”);
  }
}
finally
 Exception occurred but not handled
  class sample{
  public static void main(String args[]){
       try{
               int data=25/0;
               s.o.p(data);}
catch(NullPointerException e){s.o.p(e);}
finally{s.o.p(“finally block is always excuted”);}
s.o.p(“I love india”);
  }
}
finally
  Exception occurred but handled
 class sample{
   public static void main(String args[]){
        try{
                int data=25/0;
                s.o.p(data);}
 catch(ArithmeticException e){s.o.p(e);}
 finally{s.o.p(“finally block is always excuted”);}
 s.o.p(“I love india”);
   }
 }
throw
class sample{
  static void validate(int age){
        if(age<18)
               throw new ArithmeticException(“not valid”);
        else
               System.out.println(“welcome to vote”);
        }
  pulic static void main(String args[]){
        validate(13);
        System.out.println(“I love india”);
  }
}
Exception Propagation
class sample{
   void m(){
        int data=50/0; }
   void n(){
        m(); }
   void p(){
        try{
                n();
        }catch(Exception e){s.o.p(“Exception handled”);}
}
public static void main(String args[]){
   sample obj=new sample();
   obj.p();
   System.out.println(“I love india”);
   }
}
Exception Propagation
class sample{
   void m(){
        throw new java.io.IOException(“device error”); }
   void n(){
        m(); }
   void p(){
        try{
                n();
        }catch(Exception e){s.o.p(“Exception handled”);}
}
public static void main(String args[]){
   sample obj=new sample();
   obj.p();
   System.out.println(“I love india”);
   }
}
throws
   Checked exception can be propagated by throws keyword
  import java.io.IOException;
  class sample{
     void m() throws IOException {
          throw new IOException(“device error”); }
     void n() throws IOException{
          m(); }
     void p(){
          try{
                  n();
          }catch(Exception e){s.o.p(“Exception handled”);}
  }
  public static void main(String args[]){
     sample obj=new sample();
     obj.p();
     System.out.println(“I love india”);
     }
  }
Handling the exception if exception occurs in the
method which declares an exception
   import java.io.*;
   class M{
     void method() throws IOException {
          throw new IOException(“device error”);}
     }
     class Test{
          publi static void main(String args[]){
          try{
                 M m=new M();
                 m.method();
          }catch(Exceptionn e){s.o.p(“exception handled”);}
          System.out.println(“I love india”);}
     }
Declaring the exception if exception doesn’t
occur in the method which declares an exception
   import java.io.*;
   class M{
     void method() throws IOException {
          System.out.println(“device operation perform”);}
     }
     class Test{
          publi static void main(String args[]) throws
     IOException{
                 M m=new M();
                 m.method();
          System.out.println(“I love india”);
     }
   }
Declaring the exception if exception occurs in the
method which declares an exception
    import java.io.*;
    class M{
      void method() throws IOException {
           throw new IOException(“device error”);}
      }
      class Test{
           publi static void main(String args[]) throws
      IOException{
                  M m=new M();
                  m.method();
           System.out.println(“I love india”);}
      }
    }
Exception Handling with Method
           Overriding
 Super class method doesn’t declare an exception
 Subclass overridden method can’t declare the checked
    exception
import java.io.*;
class Parent{
    void msg(){s.o.p(“parent”);}
    }
class Child extends Parent{
    void msg() throws IOException{
       System.out.println(“child”);
       }
    pulic static void main(String args[]){
       Parent p=new Child();
       p.msg();
    }
}
 Super class method doesn’t declare an exception
 Subclass overridden method can declare the unchecked
  exception
import java.io.*;
class Parent{
    void msg(){s.o.p(“parent”);}
    }
class Child extends Parent{
    void msg() throws ArithmeticException{
       System.out.println(“child”);
       }
    pulic static void main(String args[]){
       Parent p=new Child();
       p.msg();
    }
}
Super class method declares an exception
   Subclass overridden method declares parent exception
  import java.io.*;
  class Parent{
      void msg()throws ArithmeticException{s.o.p(“parent”);}
      }
  class Child extends Parent{
      void msg() throws Exception{System.out.println(“child”); }
      pulic static void main(String args[]){
         Parent p=new Child();
         try{
         p.msg();
         }catch(Exception e){}
      }
  }
Super class method declares an exception
   Subclass overridden method declares same exception
  import java.io.*;
  class Parent{
      void msg()throws Exception{s.o.p(“parent”);}
      }
  class Child extends Parent{
      void msg() throws Exception{System.out.println(“child”); }
      pulic static void main(String args[]){
         Parent p=new Child();
         try{
         p.msg();
         }catch(Exception e){}
      }
  }
Super class method declares an exception
    Subclass overridden method declares subclass exception
   import java.io.*;
   class Parent{
       void msg()throws Exception{s.o.p(“parent”);}
       }
   class Child extends Parent{
       void msg() throws ArithmeticException{s.o.p(“child”); }
       pulic static void main(String args[]){
          Parent p=new Child();
          try{
          p.msg();
          }catch(Exception e){}
       }
   }
Super class method declares an exception
    Subclass overridden method declares no exception
   import java.io.*;
   class Parent{
       void msg()throws Exception{s.o.p(“parent”);}
       }
   class Child extends Parent{
       void msg() {s.o.p(“child”); }
       pulic static void main(String args[]){
          Parent p=new Child();
          try{
          p.msg();
          }catch(Exception e){}
       }
   }
Custom Exception
class sample{
  static void validate(int age) throws InvalidAgeException{
        if(age<18)
               throw new InvalidAgeException(“not valid”);
        else
               System.out.println(“welcome to vote”);
        }
  pulic static void main(String args[]){
        try{
        validate(13);
        }catch(Exception m){s.o.p(“Exception occured” +m);}
        System.out.println(“I love india”);
  }
}
Exception Handling

Exception Handling

  • 1.
  • 2.
    Types of Exception  Checked Exception  Unchecked Exception
  • 3.
    Common scenarios ofException Handling  ArithmeticException int a=50/0;  NullPointerException String s=null; System.out.println(s.length());
  • 4.
    Common scenarios ofException Handling  NumberFormatException String s=“abc”; int i=Integer.parseInt(s);  ArrayIndexOutOfBoundsException int a[]= new int[5]; a[10]=50;
  • 5.
    Keywords used ExceptionHandling  try  catch  finally  throw  throws
  • 6.
    try classsample { public static void main(String args[]) { int data=50/0; System.out.println(“I love india”); } }
  • 7.
    try classsample{ public static void main(String args[]){ try{ int data=50/0; } catch(ArithmeticException e) { System.out.println(e); } System.out.println(“I love india”); } }
  • 8.
    Multiple catch class sample{ public static void main(String args[]){ try{ int a[]=new int[5]; int a[5]=30/0; } catch(ArithmeticException e){s.o.p(“task 1”);} catch(ArrayIndexOutOfBoundsExceptin e){s.o.p(“task 2”);} catch(Exception e){s.o.p(“task completed”);} System.out.println(“I love india”); } }
  • 9.
    Multiple catch class sample{ public static void main(String args[]){ try{ int a[]=new int[5]; int a[5]=30/0; } catch(Exception e){s.o.p(“task completed”);} catch(ArithmeticException e){s.o.p(“task 1”);} catch(ArrayIndexOutOfBoundsExceptin e){s.o.p(“task 2”);} System.out.println(“I love india”); } }
  • 10.
    Nested try class sample{ public static void main(String args[]){ try{ try{ s.o.p(“divide”); int a=40/0; }catch(ArithmeticExceptin e){s.o.p(e);} try{ int a[]=new int[5]; int a[5]=4; }catch(ArrayIndexOutOfBoundsExceptin e){ s.o.p(e);} s.o.p(“other statements”); }catch(Exception e){s.o.p(“exception handled”);} s.o.p(“normal flow”); } }
  • 11.
    finally  Exception doesn’toccur class sample{ public static void main(String args[]){ try{ int data=25/5; s.o.p(data);} catch(NullPointerException e){s.o.p(e);} finally{s.o.p(“finally block is always excuted”);} s.o.p(“I love india”); } }
  • 12.
    finally  Exception occurredbut not handled class sample{ public static void main(String args[]){ try{ int data=25/0; s.o.p(data);} catch(NullPointerException e){s.o.p(e);} finally{s.o.p(“finally block is always excuted”);} s.o.p(“I love india”); } }
  • 13.
    finally  Exceptionoccurred but handled class sample{ public static void main(String args[]){ try{ int data=25/0; s.o.p(data);} catch(ArithmeticException e){s.o.p(e);} finally{s.o.p(“finally block is always excuted”);} s.o.p(“I love india”); } }
  • 14.
    throw class sample{ static void validate(int age){ if(age<18) throw new ArithmeticException(“not valid”); else System.out.println(“welcome to vote”); } pulic static void main(String args[]){ validate(13); System.out.println(“I love india”); } }
  • 15.
    Exception Propagation class sample{ void m(){ int data=50/0; } void n(){ m(); } void p(){ try{ n(); }catch(Exception e){s.o.p(“Exception handled”);} } public static void main(String args[]){ sample obj=new sample(); obj.p(); System.out.println(“I love india”); } }
  • 16.
    Exception Propagation class sample{ void m(){ throw new java.io.IOException(“device error”); } void n(){ m(); } void p(){ try{ n(); }catch(Exception e){s.o.p(“Exception handled”);} } public static void main(String args[]){ sample obj=new sample(); obj.p(); System.out.println(“I love india”); } }
  • 17.
    throws Checked exception can be propagated by throws keyword import java.io.IOException; class sample{ void m() throws IOException { throw new IOException(“device error”); } void n() throws IOException{ m(); } void p(){ try{ n(); }catch(Exception e){s.o.p(“Exception handled”);} } public static void main(String args[]){ sample obj=new sample(); obj.p(); System.out.println(“I love india”); } }
  • 18.
    Handling the exceptionif exception occurs in the method which declares an exception import java.io.*; class M{ void method() throws IOException { throw new IOException(“device error”);} } class Test{ publi static void main(String args[]){ try{ M m=new M(); m.method(); }catch(Exceptionn e){s.o.p(“exception handled”);} System.out.println(“I love india”);} }
  • 19.
    Declaring the exceptionif exception doesn’t occur in the method which declares an exception import java.io.*; class M{ void method() throws IOException { System.out.println(“device operation perform”);} } class Test{ publi static void main(String args[]) throws IOException{ M m=new M(); m.method(); System.out.println(“I love india”); } }
  • 20.
    Declaring the exceptionif exception occurs in the method which declares an exception import java.io.*; class M{ void method() throws IOException { throw new IOException(“device error”);} } class Test{ publi static void main(String args[]) throws IOException{ M m=new M(); m.method(); System.out.println(“I love india”);} } }
  • 21.
    Exception Handling withMethod Overriding
  • 22.
     Super classmethod doesn’t declare an exception  Subclass overridden method can’t declare the checked exception import java.io.*; class Parent{ void msg(){s.o.p(“parent”);} } class Child extends Parent{ void msg() throws IOException{ System.out.println(“child”); } pulic static void main(String args[]){ Parent p=new Child(); p.msg(); } }
  • 23.
     Super classmethod doesn’t declare an exception  Subclass overridden method can declare the unchecked exception import java.io.*; class Parent{ void msg(){s.o.p(“parent”);} } class Child extends Parent{ void msg() throws ArithmeticException{ System.out.println(“child”); } pulic static void main(String args[]){ Parent p=new Child(); p.msg(); } }
  • 24.
    Super class methoddeclares an exception  Subclass overridden method declares parent exception import java.io.*; class Parent{ void msg()throws ArithmeticException{s.o.p(“parent”);} } class Child extends Parent{ void msg() throws Exception{System.out.println(“child”); } pulic static void main(String args[]){ Parent p=new Child(); try{ p.msg(); }catch(Exception e){} } }
  • 25.
    Super class methoddeclares an exception  Subclass overridden method declares same exception import java.io.*; class Parent{ void msg()throws Exception{s.o.p(“parent”);} } class Child extends Parent{ void msg() throws Exception{System.out.println(“child”); } pulic static void main(String args[]){ Parent p=new Child(); try{ p.msg(); }catch(Exception e){} } }
  • 26.
    Super class methoddeclares an exception  Subclass overridden method declares subclass exception import java.io.*; class Parent{ void msg()throws Exception{s.o.p(“parent”);} } class Child extends Parent{ void msg() throws ArithmeticException{s.o.p(“child”); } pulic static void main(String args[]){ Parent p=new Child(); try{ p.msg(); }catch(Exception e){} } }
  • 27.
    Super class methoddeclares an exception  Subclass overridden method declares no exception import java.io.*; class Parent{ void msg()throws Exception{s.o.p(“parent”);} } class Child extends Parent{ void msg() {s.o.p(“child”); } pulic static void main(String args[]){ Parent p=new Child(); try{ p.msg(); }catch(Exception e){} } }
  • 28.
    Custom Exception class sample{ static void validate(int age) throws InvalidAgeException{ if(age<18) throw new InvalidAgeException(“not valid”); else System.out.println(“welcome to vote”); } pulic static void main(String args[]){ try{ validate(13); }catch(Exception m){s.o.p(“Exception occured” +m);} System.out.println(“I love india”); } }