3. Common scenarios of Exception Handling
ArithmeticException
int a=50/0;
NullPointerException
String s=null;
System.out.println(s.length());
4. Common scenarios of Exception Handling
NumberFormatException
String s=“abc”;
int i=Integer.parseInt(s);
ArrayIndexOutOfBoundsException
int a[]= new int[5];
a[10]=50;
6. try
class sample
{
public static void main(String args[])
{
int data=50/0;
System.out.println(“I love india”);
}
}
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. 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’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. 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. 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. 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 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”);}
}
19. 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”);
}
}
20. 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”);}
}
}