Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By superhaNds

Thread: Arithmetic exception

  1. #1
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Arithmetic exception

    XML Code:
    public class QuotientWithMethod {
           public static int quotient(int number1, int number2){
                if(number2 == 0){
                    throw new ArithmeticException("Divisor cannot be zero");
                }
                return number1 / number2;
            }
                
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter two integers: ");
            int number1 = input.nextInt();
            int number2 = input.nextInt();
           
            try{
                int result = quotient(number1, number2);
                System.out.println(number1 + " / " + number2 + " is " + result);
            }
            catch (ArithmeticException ex){
                System.out.println("Exception: an integer cannot be divided by zero");
            }
            
            System.out.println("Execution continues ...");
       }
    }
    I have few questions based on the above code.
    1) why is line 4: throw new ArithmeticException("Divisor cannot be zero"); needed? I have test without the line 4 and it still working fine
    2) why is a string needed in ArithmeticException("Divisor cannot be zero")? What is the purpose? as it can be used as comment to show divisor cannot be zero
    3) Line 19: catch (ArithmeticException ex), why ex is needed to put after Arithmetic exception?
    4) why when static is remove in line 2, it shows non-static variable cannot be referenced from a static context?

    I am doing self study on java so i do not really know the reason behind it although i have search from few sources. Hope that someone can explain it to me

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,010
    Rep Power
    6

    Default Re: Arithmetic exception

    Did you try with the value 0, so to divide by 0?

    I am not going to answer these questions, keep studying Java and the answers will come to you.

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Arithmetic exception

    1) line 4 no needed! because if ste static int method quotiend hits Arithmetic exception the try catch inside the main method catch the exception so the program print line 20 and then go on line 23
    2) every Excepetion e.x ArithmeticException has a constructor with parameter a String which save in a private variable
    3) if i have right the ex or whatever you write after ArithmeticException is the private Variable in class exception which saw us what exception we have..
    4) read here : Java: Static/Class methods

    more info : http://docs.oracle.com/javase/tutori...ons/catch.html

  4. #4
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: Arithmetic exception

    Quote Originally Posted by gimbal2 View Post
    Did you try with the value 0, so to divide by 0?

    I am not going to answer these questions, keep studying Java and the answers will come to you.
    Yes, the output is still the same for number1 == 0 and number2 == 0 even if the following part is omitted.
    Java Code:
     if(number2 == 0){
         throw new ArithmeticException("Divisor cannot be zero");
    }
    May i know what are you trying to insist here? Thanks.

  5. #5
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: Arithmetic exception

    Quote Originally Posted by ShadowWalker View Post
    1) line 4 no needed! because if ste static int method quotiend hits Arithmetic exception the try catch inside the main method catch the exception so the program print line 20 and then go on line 23
    2) every Excepetion e.x ArithmeticException has a constructor with parameter a String which save in a private variable
    3) if i have right the ex or whatever you write after ArithmeticException is the private Variable in class exception which saw us what exception we have..
    4) read here : Java: Static/Class methods

    more info : The catch Blocks (The Java™ Tutorials > Essential Classes > Exceptions)
    Thanks for reply. I am still digesting the reply on 4. For 1 though, I have discovered that actually not only line 4, line 3 to line 5 can be completely omitted and still yield the same output. Am I right ? Also, if it is not needed, why the example in the book include line 3 to line 5. Hoping to hear more explaination.

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Arithmetic exception

    As gimbal2 said on reply #2, have you tried to divide a number by zero? For example what happen if you do this in your code:

    Java Code:
    int i = 10 / 0;
    It will the a java.lang.ArithmeticException because you cannot divide by zero. I think that is the intention of your code from line 3 to 5, to protect you from the ArithmeticException. But yeah, I know it is duplicated because you catch the exception in the main method anyway. So you have to choose where you will handle the exception. Is it in the quotient method or in the main method.

  7. #7
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: Arithmetic exception

    +
    Quote Originally Posted by wsaryada View Post
    As gimbal2 said on reply #2, have you tried to divide a number by zero? For example what happen if you do this in your code:

    Java Code:
    int i = 10 / 0;
    It will the a java.lang.ArithmeticException because you cannot divide by zero. I think that is the intention of your code from line 3 to 5, to protect you from the ArithmeticException. But yeah, I know it is duplicated because you catch the exception in the main method anyway. So you have to choose where you will handle the exception. Is it in the quotient method or in the main method.
    I not really understand with the terms to protect you from the ArithmeticException. Mind to clarify on this.

    Is there any difference in handling the exception in main method or in the call? or it is just depends on one preference over another?

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,010
    Rep Power
    6

    Default Re: Arithmetic exception

    Quote Originally Posted by dojob View Post
    +

    I not really understand with the terms to protect you from the ArithmeticException. Mind to clarify on this.
    Neither do I to be honest. The code's intention to me seems to prevent you from getting a vague divide by zero error from Java an in stead turn that into an exception with a more human-readable message.

    Is there any difference in handling the exception in main method or in the call? or it is just depends on one preference over another?
    There is no real answer to that - it depends on the code and the situation. Sometimes you want to catch an exception as soon as it is caused, sometimes you want to let an exception just go to the top. Generally speaking when you let an exception go to the top, it is one you cannot and should not handle because something is actually and really wrong and you can't ever recover from it.

    An example of an exception you likely want to catch right away: a NumberFormatException when you are trying to ask the user to input a number. When he/she inputs something that is not a number you will want to catch the exception and show an error ("Oi, input a number you dope!")

    An example of an exception you likely do not want to catch right away but want to "bubble" up to for example the main(): an IOException when trying to connect to a server. The server is down or the connection details are just bad (ex: bad configuration file), whatever the case that problem will need to be corrected before another attempt can be made to connect to the server from the application. At most you will want to show an error message to the user, or write the error to a log file.
    dojob likes this.

  9. #9
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Arithmetic exception

    In a situation like your example

    Java Code:
         public static int quotient(int number1, int number2){
                if(number2 == 0){
                    throw new ArithmeticException("Divisor cannot be zero");
                }
                return number1 / number2;
            }
    You should not throw arithmetic exception. If an argument passed on a method is illegal, throw an IllegalArgumentException, it is one of the most common seen and easy to understand.
    Also, it is not necessary to throw the arithmetic exception, it's a runtime exception and you would have it regardless if number 2 was 0 at some point and it is dealt with in the main, maybe it is there to understand things
    better.
    dojob likes this.

Similar Threads

  1. help with a Loop(arithmetic)
    By derb2k2 in forum New To Java
    Replies: 6
    Last Post: 01-26-2012, 01:15 PM
  2. Arithmetic using doubles
    By Black_Eye in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 08:35 AM
  3. Abbreviated Arithmetic
    By SweetLD215 in forum New To Java
    Replies: 19
    Last Post: 10-13-2010, 12:46 AM
  4. Polynomials Arithmetic
    By thisisIT in forum New To Java
    Replies: 3
    Last Post: 03-09-2010, 01:27 PM
  5. Arithmetic Stacks
    By unc123w in forum New To Java
    Replies: 22
    Last Post: 10-21-2008, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •