
can we use inbuilt exception variable with throw keyword in java

The above code can also be written as follows :
public class Test
{
ArithmeticException ae;
public int divide(int numerator,int denominator)
{
if(second==0)
{
throw ae;
}
return numerator/denominator;
}
public static void main(String[] args)
{
Test test = new Test();
test.ae = new ArithmeticException("Second Number must not be zero");
int result = test.divide(4, 0);
System.out.println(result);
}
}

hi friend, I think we can use the inbuilt exception variable with throw keyword in java.
I hope following sample code will be helpful for you.
public class Test
{
static ArithmeticException ae;
static int divide(int first,int second)
{
if(second==0)
{
throw ae;
}
return first/second;
}
public static void main(String[] args)
{
ae = new ArithmeticException("Second Number must not be zero");
try
{
int result = divide(4, 0);
System.out.println(result);
}
catch (Exception e)
{
//e.printStackTrace();
System.out.println(ae);
//System.out.println(e);
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.