Monday, April 21, 2008

EXCEPTION

MULTICATCH

class Multicatch
{
public static void main(String[] arg)
{
try
{
int a=arg.length;
int b=10/a;
int c[]={1,2};
System.out.println(c[5]);
}
catch(ArithmeticException e)
{
System.out.println("Divide by zero"+e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index out of bounds"+e);
}
System.out.println("after try/catch block");
}
}


NESTED TRY

class NestedTry
{
public static void main(String args[])
{
try
{
int x =args.length;
System.out.println("x"+x);
int y=(2/x);
System.out.println("y"+y);
try
{
if(x==1)
x=x/(x-x);
if(x==2)
{
int c[]={1};
c[2]=32;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Out of Bounds exception"+e);
}
catch(ArithmeticException e)
{
System.out.println("Divide by zero"+e);
}
}
catch(ArithmeticException ae)
{
System.out.println("Caught an error"+ae);
}
}
}

THROW

class Throwdemo
{
static void demoproc()
{
try
{
throw new NullPointerException("Demo");
}
catch(NullPointerException e)
{
System.out.println("Caught inside demoproc");
throw e;
}
}
public static void main(String[] ar)
{
try
{
demoproc();
}
catch(NullPointerException e)
{
System.out.println("Recaught"+e);
}
}
}

TRY CATCH

class Exception
{
public static void main(String ar[])
{
int a,b;
try
{
a=0;
b=26/a;
}
catch(ArithmeticException e)
{
System.out.println("Division by zero"+e);
}
System.out.println("Exception handling");
}
}

No comments: