Exception Handling2
Exception Handling2
BY
TANUSREE SAHA
EXAMPLE MULTIPLE CATCH BLOCK:
public class MB2 {
try{
String s=null;
System.out.println(s.length());
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
HERE EXCEPTION IS BASICCALLY EXCEPTION ON Nullpointexception BUT NO CATCH BLOCK IS THERE TO
HANDEL IT SO IS HANDLED BY GENERIC CATCH
EXAMPLE MULTIPLE CATCH BLOCK:
public class MB3 {
try{
String s=null;
System.out.println(s.length());
}
catch(NullPointerException e)
{
System.out.println("null point Exception occurs");
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
Java Nested try block
....
try
{
statement 1;
statement 2;
try
{
statement 1;
statement 2;
}
catch(Exception e)
{
}
}
catch(Exception e)
{
}
....
EXAMPLE: NESTED TRY
class NTC1{
public static void main(String args[]){
try{
try{
System.out.println("going to divide");
int b =39/0;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
try{
int a[]=new int[5];
a[5]=4;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
System.out.println("other statement");
}
catch(Exception e)
{
System.out.println("handeled");
}
System.out.println("normal flow..");
}
EXCEPTION HANDLING
BY
TANUSREE SAHA
Java Finally block – Exception handling
Case 1: Let's see the java finally example where exception doesn't
occur.
class FB2{
public static void main(String args[]){
try{
int data=25/5;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
Usage of Java finally
Case 2: Let's see the java finally example where exception
occurs and not handled.
class FB3{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
Usage of Java finally
Case 3: Let's see the java finally example where exception
occurs and handled.
public class FB4{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}
catch(ArithmeticException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
Few Important points regarding finally block
class FIR
{
public static void main(String args[])
{
System.out.println(FIR.myMethod());
}
public static int myMethod()
{
try {
return 112;
}
finally {
System.out.println("This is Finally block");
System.out.println("Finally block ran even after return statement");
}
}
}
Throw & Throws
EXCEPTION HANDLING
BY
TANUSREE SAHA
Java throw exception
System.out.println("normal flow...");
}
}
Case2: You declare the exception
System.out.println("normal flow...");
}
}
B)Program if exception occurs
import java.io.*;
class M{
void method()throws IOException{
throw new IOException("device error");
}
}
class Testthrows4{
public static void main(String args[])throws IOException{//
declare exception
M m=new M();
m.method();
System.out.println("normal flow...");
}
}
Difference between throw and throws in Java
throw throws
class UDE1{
public static void main(String args[]){
try{
System.out.println("Starting of try block");
// I'm throwing the custom exception using throw
throw new MyException("This is My error Message");
}
catch(MyException exp){
System.out.println("Catch Block") ;
System.out.println(exp) ;
}
}
}
Another Example of Custom Exception
class InvalidProductException extends Exception
{
public InvalidProductException(String s)
{
// Call constructor of parent Exception
super(s);
}
}
public class UDE2
{
void productCheck(int weight) throws InvalidProductException{
if(weight<100){
throw new InvalidProductException("Product Invalid");
}
}
public static void main(String args[])
{
UDE2 obj = new UDE2();
try
{
obj.productCheck(60);
}
catch (InvalidProductException ex)
{
System.out.println("Caught the exception");
System.out.println(ex.getMessage());
}
}
}
Output:
assignment
Write java program o following:
Arithmetic exception
ArrayIndexOutOfBounds Exception
NumberFormat Exception
StringIndexOutOfBound Exception
NullPointer Exception
Thank You