-1

This is the code snippet:

public void actionPerformed(ActionEvent e){
        int i1,i2;

        try{
            if(e.getSource()==b1){
            .
            .
            .

            else if(e.getSource()==b4){
                double i1,i2;
                i1=Integer.parseInt(t1.getText());
                i2=Integer.parseInt(t2.getText());
                i1=i1/i2;
                l7.setText(i1+"");
            }
            else if(e.getSource()==b5){
                i1=Integer.parseInt(t1.getText());
                i2=Integer.parseInt(t2.getText());
                i1=i1%i2;
                l7.setText(i1+"");
            }
        }
        catch(ArithmeticException ex2){
            l7.setText("Debugging?");
            JOptionPane.showMessageDialog(null,"Divide by zero exception!!");
            //*WHY THIS SECTION IS NEVER BEING EXECUTED AFTER PERFORMING A DIVIDE BY ZERO i.e. ARITHMETIC EXCEPTION!*
        }
        catch(Exception ex){
            JOptionPane.showMessageDialog(null,"Enter Values First!!");
        }
    }

The JRE is never executing the Arithmetic Exception catch statement, why?

Yes, it is handing it but it not producing the output that I expect it to produce! It is automatically displaying, "Infinity" & "NaN" on my Java application! Thanks!

2 Answers2

0

Check these lines of code:

        } else if (e.getSource() == b4) {
            double i1, i2; // Comment this line to make it work like you need
            i1 = Integer.parseInt(t1.getText());
            i2 = Integer.parseInt(t2.getText());
            i1 = i1 / i2;
            l7.setText(i1 + "");
        }

You have redeclared i1 and i2 to be double. The Double type defines inbuilt values for Infinity and NaN. That's the reason your code is not executing your ArithmeticException catch block.

Simply comment the double i1, i2; line to make it work like you need.

Update

If you want to show an error message, simply put a check:

        } else if (e.getSource() == b4) {
            double i1, i2;
            i1 = Integer.parseInt(t1.getText());
            i2 = Integer.parseInt(t2.getText());
            if(i2==0){
                throw new ArithmeticException("Cannot divide by zero");
            }
            i1 = i1 / i2;
            l7.setText(i1 + "");
        }

Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31
  • So what Can I do to fix it? Float is also giving same output! –  Feb 14 '17 at 10:39
  • Simply comment the `double i1, i2;` line to make it work like you need. – anacron Feb 14 '17 at 10:41
  • And if I remove try catch, then it wont handle it –  Feb 14 '17 at 10:42
  • But I want the calculator to give me decimals output too –  Feb 14 '17 at 10:43
  • Why don't you validate the input whether `i2 ==0` ? – anacron Feb 14 '17 at 10:49
  • Then what is the role of Arithmetic Exception class? If I start validating all the error conditions by myself! –  Feb 14 '17 at 10:51
  • Yes, but I want the control flow to enter that catch section and execute those codes too! Also, why it is not executing the catch statement by itself? –  Feb 14 '17 at 11:21
0

Yes! Double and float have their own inbuilt values for infinity. There might be some other issues in your code that I'm not aware about.

Check these edits:

else if(e.getSource()==b4){
            double i1,i2;
            i1= Integer.parseInt(t1.getText());
            i2= Integer.parseInt(t2.getText());
            if(i1!=0 && i2 == 0){
                throw new ArithmeticException();
            }
            else if(i2 == 0 && i1 == 0) {
                throw new ArithmeticException();
            }
            i1= i1/i2;
            l7.setText(i1+"");
        }

This throw new ArithmeticException(); will force your code to execute the section that you are willing to execute!

Also, check this link: https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

Hope this helps!