Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions java question on exception

Have Programming Question? Ask it here!
 
 
 


priyanka sobani
java question on exception
2 Answer(s)      a year and 7 months ago
Posted in : Java Interview Questions


can we use inbuilt exception variable with throw keyword in java


Advertisement
View Answers

January 9, 2013 at 4:12 PM


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);        
    }
}


January 9, 2013 at 1:23 PM


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);
        }
    }
}


Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com