java | Throw and Rethrow Example

//Example of Throw and Rethrow


class ThrowDemo
{

static void call()
{
try
{

throw new NullPointerException("this Nullpointerexception fired by me");


}

catch(NullPointerException e)
{


System.out.println("I Handeled my throw exception "+e);


throw e;// rethrowing Exception Handled by main catch

}

}
static void call1()
{
try
{

throw new ArithmeticException("this Arithmeticexception fired by me");


}


catch(ArithmeticException e)
{


System.out.println("I Handeled my throw exception "+e);


}


}
}


class Test
{

public static void main(String ar[])
{
try{
ThrowDemo.call();
}


catch(Exception e)
{


System.out.println("exception handeled for rethrow");

}


ThrowDemo.call1();

}

}
Reactions:

0 comments:

Post a Comment