1

I am kinda new with the whole catching-handling exceptions concept and I was wondering why the throws ArithmeticException doesn't produce an exception error message (in this case/by zero) on exit, but instead during compilation.

Shouldn't it compile normally and then show the error message at the screen? What am I doing wrong?

public class Exception_Tester 
{ 
    public static void main(String args[]) 
    { 
         Exception_Tester et = new Exception_Tester(); 
         int x1; 
         int x2; 
         x1 = 5; 
         x2 = 0; 
         et.printResults(x1, x2); 
    } 

    void printResults(int a, int b) throws ArithmeticException 
    { 
         System.out.println("Add: "+(a+b)); 
         System.out.println("Sub: "+(a-b)); 
         System.out.println("Mul: "+(a*b));
         System.out.println("Div: "+(a/b));
    }  
} 
NoNaMe
  • 6,020
  • 30
  • 82
  • 110
JimS
  • 349
  • 1
  • 4
  • 19
  • It shows the error message when you run the program. It doesn't cause a compilation error because `ArithmeticException` is not a checked exception. Google checked exception. – Paul Boddington Apr 17 '15 at 18:33
  • When I enclose the System.out.println("Div: "+(a/b)); into a try catch block it works as intended. What I actually want is to use this kind of exception handling but the error message appear on screen during runtime and not on compilation – JimS Apr 17 '15 at 18:37
  • I'm sorry but I don't understand your question. Are you saying you want the compiler to warn you about the division by zero problem before you run the program? – Paul Boddington Apr 17 '15 at 18:47
  • The opposite. I want it to compile normally and give me the / by zero arithmeticexception message on exit along with the other results. – JimS Apr 17 '15 at 18:50
  • I would do something like this `if (b == 0) System.out.println("Error: you cannot divide by 0"); else System.out.println("Div: " + (a/b));` – Paul Boddington Apr 17 '15 at 18:54

3 Answers3

0

Checked Exception : these exception will throw error while compilation if you have not handled these exceptions. Unchecked Exception : you will get error only at RunTime if you have not handled.

ArithmaticException is unchecked exception so you will get exception at runtime.

if you are using try-catch block then you have to use

printStackTrace()

method to print exception stack trace.

as :

try{
    System.out.println("Add: "+(a+b)); 
    System.out.println("Sub: "+(a-b)); 
    System.out.println("Mul: "+(a*b));
     System.out.println("Div: "+(a/b));
}
catch(ArithmeticException e){
    e.printStackTrace();
}
Prashant
  • 2,556
  • 2
  • 20
  • 26
0

Take a look at the following picture:

exceptions categories

As you can see a few of the exception classes are in a bold font to draw our attention to them. Here is the editors explanations about these categories of exceptions

  • Conditions that can readily occur in a correct program are checked exceptions. Concretely these kind of exceptions are <> by the compiler and he can correctly assess the eventuality of their occurence and declare a compilation error when the circumstances correspond to it. As you can see from the picture, NullPointerException is not directly under this category: these are the exception that directly extends the Exception class.

  • Severe problems that normally are treated as fatal or situations that probably reflect program bugs are unchecked exceptions.

  • Fatal situations are represented by the Error class.

  • Probable bugs are represented by the RuntimeException class. This is the case for exemple with the exceptions that extends the RuntimeException class. NullPointerException is one of them. In most of the cases of this kind of exception the compiler is not able to assess @compile time that they will cause an exception, since there is a strong dependency to the dynamic state of the application

Here is a simple illustration:

I have created two exceptions classes one that extends Exception

public class Exception1 extends Exception {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

}

and one that extends RuntimeException

public class Exception2 extends RuntimeException {

    private static final long serialVersionUID = 4595191052237661216L;

}

Then I have the following NewTester class

public class NewTester {

    public static void methodA() throws Exception1 {

        throw new Exception1();
    }

    public static void methodB() throws Exception2 {

        throw new Exception2();
    }

    public static void main(String[] args) {
        // methodA();
        methodB();
    }
}

I have purposefully commented the call to methodA.In this state you don't have any compilation error because the method that is called methodBthrows a RuntimeException which is unchecked. But if you would change this code by uncommenting the call to methodA and commenting the call to methodB you will have a compilation error, because methodA throws a checked exception

I hope this helps

alainlompo
  • 4,414
  • 4
  • 32
  • 41
0

I executed your code as it is

public class Exception_Tester 
{ 
public static void main(String args[]) 
{ 
 Exception_Tester et = new Exception_Tester(); 
 int x1; 
 int x2; 
 x1 = 5; 
 x2 = 0; 
 et.printResults(x1, x2); 
} 
void printResults(int a, int b) throws ArithmeticException 
{ 
  System.out.println("Add: "+(a+b)); 
  System.out.println("Sub: "+(a-b)); 
  System.out.println("Mul: "+(a*b));
  System.out.println("Div: "+(a/b));
}  
} 

And it compiles fine without any error or exception, and as per your requirement, it is throwing ArithmeticException at run time only when System.out.println("Div: "+(a/b)); statement is encountered.

So I don't See any Problem there!

Pinky
  • 88
  • 6
  • Yeah I figured out it was me who misunderstood the "throws ArithmeticException" statement. The code is fine :) – JimS Apr 18 '15 at 16:58