Exception Handling

29
Exception Handling Presented By, Vinod Kumar V H

Transcript of Exception Handling

Page 1: Exception Handling

Exception Handling

Presented By,Vinod Kumar V H

Page 2: Exception Handling

Types of Exception

Checked ExceptionUnchecked Exception

Page 3: Exception Handling

Common scenarios of Exception HandlingArithmeticException int a=50/0;

NullPointerException

String s=null; System.out.println(s.length());

Page 4: Exception Handling

Common scenarios of Exception HandlingNumberFormatException String s= “abc”;

int i=Integer.parseInt(s);

ArrayIndexOutOfBoundsException

int a[]= new int[5]; a[10]=50;

Page 5: Exception Handling

Keywords used Exception Handling

trycatchfinallythrowthrows

Page 6: Exception Handling

tryclass sample{

public static void main(String args[]){

int data=50/0;System.out.println(“I love

india”);}

}

Page 7: Exception Handling

tryclass 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”);

}}

Page 8: Exception Handling

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”);

}}

Page 9: Exception Handling

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”);

}}

Page 10: Exception Handling

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”);}

}

Page 11: Exception Handling

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”);

}}

Page 12: Exception Handling

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”);

}}

Page 13: Exception Handling

finally Exception occurred but handledclass 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”);

}}

Page 14: Exception Handling

throwclass sample{

static void validate(int age){if(age<18)

throw new ArithmeticException(“not valid”);

elseSystem.out.println(“welcome to vote”);

}pulic static void main(String args[]){

validate(13);System.out.println(“I love india”);

}}

Page 15: Exception Handling

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”);}

}

Page 16: Exception Handling

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”);}

}

Page 17: Exception Handling

throws Checked exception can be propagated by throws keywordimport 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”);}

}

Page 18: Exception Handling

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”);}}

Page 19: Exception Handling

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”);}

}

Page 20: Exception Handling

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”);}}

}

Declaring the exception if exception occurs in the method which declares an exception

Page 21: Exception Handling

Exception Handling with Method Overriding

Page 22: Exception Handling

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();

}}

Page 23: Exception Handling

Super class method doesn’t declare an exceptionSubclass 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();

}}

Page 24: Exception Handling

Super class method declares an exceptionSubclass overridden method declares parent exceptionimport 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){}

}}

Page 25: Exception Handling

Super class method declares an exceptionSubclass overridden method declares same exceptionimport 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){}

}}

Page 26: Exception Handling

Super class method declares an exceptionSubclass overridden method declares subclass

exceptionimport 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){}

}}

Page 27: Exception Handling

Super class method declares an exceptionSubclass overridden method declares no exceptionimport 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){}

}}

Page 28: Exception Handling

Custom Exceptionclass sample{

static void validate(int age) throws InvalidAgeException{if(age<18)throw new InvalidAgeException(“not valid”);elseSystem.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”);}

}

Page 29: Exception Handling

Thank

‘U’