Exception Handling

712 views
497 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
712
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Exception Handling

  1. 1. Presented By,Vinod Kumar V H
  2. 2. Types of Exception  Checked Exception  Unchecked Exception
  3. 3. Common scenarios of Exception Handling ArithmeticException int a=50/0; NullPointerException String s=null; System.out.println(s.length());
  4. 4. Common scenarios of Exception Handling NumberFormatException String s=“abc”; int i=Integer.parseInt(s); ArrayIndexOutOfBoundsException int a[]= new int[5]; a[10]=50;
  5. 5. Keywords used Exception Handling  try  catch  finally  throw  throws
  6. 6. try class sample { public static void main(String args[]) { int data=50/0; System.out.println(“I love india”); } }
  7. 7. 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”); } }
  8. 8. Multiple catchclass 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. 9. Multiple catchclass 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. 10. Nested tryclass 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. 11. 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”); }}
  12. 12. 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”); }}
  13. 13. 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”); } }
  14. 14. throwclass 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. 15. Exception Propagationclass 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. 16. Exception Propagationclass 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. 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. 18. Handling the exception if exception occurs in themethod 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. 19. Declaring the exception if exception doesn’toccur 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. 20. Declaring the exception if exception occurs in themethod 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. 21. Exception Handling with Method Overriding
  22. 22.  Super class method doesn’t declare an exception Subclass overridden method can’t declare the checked exceptionimport 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. 23.  Super class method doesn’t declare an exception Subclass overridden method can declare the unchecked exceptionimport 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. 24. 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){} } }
  25. 25. 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){} } }
  26. 26. 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){} } }
  27. 27. 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){} } }
  28. 28. Custom Exceptionclass 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”); }}

×