Do not execute Try / Catch block in Form

0

I made a basic calculator with forms and when wanting to manage the ArithmeticException() by zero it seems not to enter the catch and execute the sentences that I define for such a case (neither System.out.println, nor JOptionPane.showMessage, nor jtxt_calcu.setText).

  • Equal button (=) that executes the'calc' method

    private void btn_igualActionPerformed(java.awt.event.ActionEvent evt) {                                          
    
    memoria2=jtxt_calcu.getText();
    jtxt_todo.setText(jtxt_todo.getText()+" "+ memoria2);
    try {
        jtxt_calcu.setText(this.calc(memoria1, signo, memoria2));
    }
    catch (ArithmeticException e) {
        jtxt_calcu.setText("Error al dividir por cero");
        System.out.println("Error al dividir por cero");
        JOptionPane.showMessageDialog(null, "Error al dividir por cero");
    }
    flag=true;   
    } 
    
  • .calc method

    public String calc (String memoria1, String signo, String memoria2)throws ArithmeticException {
    Double resultado=0.0;
    String respuesta = "";
    if (signo.equals("+")){
        resultado = Double.parseDouble (memoria1) + Double.parseDouble (memoria2);
        respuesta = resultado.toString();
    }
    if (signo.equals("-")){
        resultado = Double.parseDouble (memoria1) - Double.parseDouble (memoria2);
        respuesta = resultado.toString();
    }
    if (signo.equals("*")){
        resultado = Double.parseDouble (memoria1) * Double.parseDouble (memoria2);
        respuesta = resultado.toString();
    }
    if (signo.equals("/")){
        resultado = Double.parseDouble(memoria1) / Double.parseDouble(memoria2);
        respuesta = resultado.toString();
    }
    if (respuesta.endsWith(".0")){
        respuesta = respuesta.replace(".0","") ;
    }
    return  respuesta;
    }
    

All this is within the Source of the calculator.

The output of the program when divided by ZERO is always'Infinity'in my text field jtxt_calcu

Any ideas on how to make this exception handling work?

    
asked by Die Duro 23.08.2017 в 22:51
source

1 answer

3

Very good question ...

The question is:

  • The int are encoded in a binary complement to 2, which allows to represent all the integers between (-2 ^ 31) and (2 ^ 31 - 1)

  • The long is encoded in binary complement to 2, which allows to represent all the integers between (-2 ^ 63) and (2 ^ 63 - 1)

  • In contrast, floating-point numbers ( float and double ) are encoded with the IEEE standard 754 The difference that matters to us is that this standard defines special values like 0 , Infinity and NaN ("Not A Number", it is not a number) . And the standard defines that when you divide a floating point number by 0 you get infinite; here explains a bit the reasoning of this definition

So:

  • When you divide by zero in an integer operation ( int or long ) you get a value that you can not represent in the variable. The only option to not leave the variable with an "invalid" value is to throw the exception.

  • When you divide by zero in a floating point operation ( float or double ) the standard gives you a value to assign to the variable, and tells you that this is the correct result. There is no need to throw an exception.

If you want to control that possibility, the simplest thing would be to check if Double.isInfinite(valor) returns true or false ; not only will you check the case that you divide by zero but also any other overflow.

    
answered by 23.08.2017 / 23:11
source