Google Ads

Thursday, May 21, 2009

EXCEPTION HANDLING -Program

class exp extends Exception

{

exp(String msg)

{

super(msg);

}

}

class exception

{

public static void main(String args[])

{

int b=4,c=0,s;

try

{

s=b/c;

}

catch(ArithmeticException e)

{

System.out.println("Divide by zero error occured"+e);

}

try

{

int x=-1;

throw new exp("Number is invalid");

}

catch(exp e)

{

System.out.println("Number is invalid"+e);

e.getMessage();

}

try

{

int a[]={1};

a[10]=2;

}

catch(ArrayIndexOutOfBoundsException e)

{

System.out.println("Array index out of bound error has occured"+e);

}

finally

{

System.out.println("Error occured");

}

}

}

OUTPUT:

Z:\jdk1.5\bin>javac exception.java

Z:\jdk1.5\bin>java exception


Divide by zero error occurred java.lang.ArithmeticException: / by zero


Number is invalid exp: Number is invalid


Array index out of bound error has occurred java.lang.ArrayIndexOutOfBoundsException:


10


Error occurred

No comments:

Post a Comment