java 中的异常处理优化

Exception Handling optimization in java

我想创建一个程序来处理除以两个整数时可能发生的 3 种异常,如果它触发异常,则要求用户更正输入。只有在没有触发异常的情况下,代码才会执行​​。下面的代码可以工作,但我觉得它太没有优化了。除了 while 循环之外,没有其他方法可以不断检查异常吗?

import javax.swing.JOptionPane;

public class DivisionExceptions {

public int divide(int num, int den) {
    return num/den;
}

public static void main(String[] args) {
    int num = 0,
        den = 0,
        a   = 0,
        b   = 0,
        c   = 0;

    DivisionExceptions div = new DivisionExceptions();

        while(a == 0) {
            try {
                num = Integer.parseInt(JOptionPane.showInputDialog("Introduce the first int"));
                a++;
            }
            catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again.");
            }
        }
    while (c == 0) {    
        b = 0;
        while(b == 0) {
            try {
                den = Integer.parseInt(JOptionPane.showInputDialog("Introduce the second int"));
                b++;
            }
            catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again.");
            }
        }

        try {
            JOptionPane.showMessageDialog(null, "Result of dividing: " + num + "/" + den + " is " + div.divide(num, den));
            c++;
        }
        catch (ArithmeticException e) {
            JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again.");
        }
    }
}

}

可以简化为:

public static void main(String[] args) {
    int num = 0, den = 0;

    DivisionExceptions div = new DivisionExceptions();

    while(true) {
        try {
            num = Integer.parseInt(JOptionPane.showInputDialog("Introduce the first int"));
            break;
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again.");
        }
    }

    while (true) {
        try {
            den = Integer.parseInt(JOptionPane.showInputDialog("Introduce the second int"));
            JOptionPane.showMessageDialog(null, "Result of dividing: " + num + "/" + den + " is " + div.divide(num, den));
            break;
        } catch (NumberFormatException | ArithmeticException e) {
            JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again.");
        }
    }
}

~

嗯,您的代码可以使用一些重构。

public class DivisionExceptions {

public int divide(int num, int den) {
    return num / den;
}

public static void main(String[] args) {
    int num = 0,
            den = 0,
            a = 0,
            b = 0,
            c = 0;

    DivisionExceptions div = new DivisionExceptions();
    num = getNum(a, "Introduce the first int");
    den = getNum(b, "Introduce the second int");
    while (c == 0) {
        try {
            JOptionPane.showMessageDialog(null, "Result of dividing: " + num + "/" + den + " is " + div.divide(num, den));
            c++;
        } catch (ArithmeticException e) {
            JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again.");
        }
    }
}

private static int getNum( int loopParam, String message) {
    int num = 0;
    while (loopParam == 0) {
        try {
            num = Integer.parseInt(JOptionPane.showInputDialog(message));
            loopParam++;
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again.");
        }
    }
    return num;
}
}

我也允许自己从 while(c==0) 循环中提取 den 计算,因为它总是计算相同的值但计算 n 次,所以你在这里获得了一些优化.如果您能提供更多关于为什么将所有参数预定义为 0 的信息,也许我可以为 while(c==0) 循环找到一些解决方案。如果你使用 java 8 你也可以将你的 while 循环提取到另一个方法并将一些函数作为参数。