java question on exception

Ads
 

java question on exception

can we use inbuilt exception variable with throw keyword in java

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

Ads









Related Tutorials/Questions & Answers:
java question on exception
java question on exception  can we use inbuilt exception variable with throw keyword in java
Question on Checked Exception
Question on Checked Exception   why checked exception force to put try and catch block ? Please send me answer
Advertisements
Exception Java
Exception Java  Hi, What are the Exception Java? Thanks   Hi, Read it at Java Exception. Thanks
java exception
java exception   define an exception called no match exception tat z... { public static void main(String[] args) throws Exception { String z="Hello"; if(!z.equals("India")){ throw new Exception
Java Exception
Java Exception  Explain about checked and unchecked Exceptions... this exception will be caught by a well-written application and will also prompt... to the constructor. Well in this case the exception could be caught by the application
null pointer exception in java
null pointer exception in java  What is null pointer exception in Java and when it occurs? Show it with an example.Thanks!   Java Null Pointer Exception
java exception handle
java exception handle  How error can be handled in the exception class?? can we handle fatal error in java
Question in Java ??
Question in Java ??   Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number squrfoot
Question in Java ??
Question in Java ??  Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number 0 ,2, . . . ,18,20 squrfoot 00000 1.4142 5.2426 5.4721
Question in Java ??
Question in Java ??  Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number 0 ,2, . . . ,18,20 squrfoot 00000 1.4142 5.2426 5.4721
Question Java>?
Question Java>?  **ï??· Create a new class called Bank and in the main method use class Account (lab 4) to create an array of 10 accounts. ï??· Enter data for all accounts (account number,name, balance). ï??· Add
java question :)
java question :)  write java program to use vector in ArrayList with add, remove,sort   import java.util.Vector; public class... Element MUAYAD"); vc.add("Vector Element JAVA"); vc.add("Vector Element
check null exception java
check null exception java  check null exception java - How to check the null exception on Java?   The null pointer exception in java occurs... on it. See the example null pointer exception
java question
java question   Create a class called Complex to perform arithmetic operations with complex numbers. 1- Use double variables to represent the fields of the class. 2- Provide a no-argument constructor with default values in case
Java Question
Java Question  how can we increment the value of database SQL by using the java servlet program ? plz help me   Hi Friend, Try...+")"); out.println("Id is incremeneted!"); } catch(Exception e
java question
java question  Implement a class called ShortAddress that has the following attributes: firstName, secondName, and phoneNumber. Now implement... are including utility classes. Use exception handling to make the program robust. use
java question
java question  the purpose of defining multiple constructor in java
java question
java question  find the 127 twin pair in java
JAVA QUESTION
JAVA QUESTION  How to view image on Frame in swing(or)awt in Java
JAVA QUESTION
JAVA QUESTION  How to view image on Frame in swing(or)awt in Java
JAVA QUESTION
JAVA QUESTION  How to view image on Frame in swing(or)awt in Java
java question
java question  what is the difference between path and classpath in java
java question
java question  How to find the longest and shortest line from the file in case of java and java program should read the filename from the keyboard
java question
java question  Please, give me sample program for insert Audio(sound) on Frame in swing in java
java question
java question  how to write a java program that, given a friend name , display the phone number and the email address for that friend
List of Java Exception
List of Java Exception       Exception in Java are classified on the basis of  the exception handled by the java compiler. Java consists of the following type of built
java question
java question  anyone provide the code to call c++ function from java   Here you will find details how to implement the c++ into the java. http://www.javaworld.com/javatips/jw-javatip23.html
Java Question
Java Question  can u give me one expample in which conditions we... in java includes only abstract methods i.e. methods have signatures only (or we... in java defines that it must be implemented by any class to make use of it. One
Java Stream Write Exception
Java Stream Write Exception  I am doing socket programming. I have...(Exception e) { System.out.println("Exception :: "+e); } When the line out.writeUTF(data1) is executed and catch catches it and shows the exception
java question
java question  input any number and check if sum of alternate digit is palindrome
java question
java question  what are the exactly J2EE concepts.. struts and hybernates coming under which catagiry

Ads