Monday, August 29, 2011

Exception Handling in java


//ExceptionHandelingExample.java

public class ExceptionHandelingExample
{
public static void main(String args[])
{
int a=10,b=0;
try
{
int c = a/b;
System.out.println(c);
}
catch (NumberFormatException ne) {
System.out.println("I am in NumberFormatException catch block and the error is "+ne.getMessage());
}
catch (ArrayIndexOutOfBoundsException are) {
System.out.println("I am in ArrayIndexOutOfBoundsException catch block and the error is "+are.getMessage());
}
catch (ArithmeticException ae) {
System.out.println("I am in ArithmeticException catch block and the error is "+ae.getMessage());
}
catch (Exception e) {
System.out.println("I am in Exception catch block and the error is "+e.getMessage());
}

finally
{
System.out.println("I am in finally block");
}
}
}

Output =>
I am in ArithmeticException catch block and the error is / by zero
I am in finally block

No comments:

Post a Comment