Exception Handling

Post on 20-May-2015

359 views 3 download

Tags:

Transcript of Exception Handling

Exception Handling

Presented By,Vinod Kumar V H

Types of Exception

Checked ExceptionUnchecked Exception

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

NullPointerException

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

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

int i=Integer.parseInt(s);

ArrayIndexOutOfBoundsException

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

Keywords used Exception Handling

trycatchfinallythrowthrows

tryclass sample{

public static void main(String args[]){

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

india”);}

}

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

}}

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

}}

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

}}

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

}

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

}}

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

}}

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

}

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

}

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

}

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

}

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

Exception Handling with Method Overriding

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

}}

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

}}

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

}}

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

}}

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

}}

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

}}

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

}

Thank

‘U’