在java中的异常处理优化
问题描述:
我想创建一个程序来处理分割两个整数时发生的3个可能的异常,要求用户在触发异常时纠正输入。代码仅在没有触发例外的情况下执行。下面的代码有效,但我觉得它太不优化了。除了循环之外,没有其他方法可以连续检查异常吗?在java中的异常处理优化
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.");
}
}
}
〜
+0
这就是我一直在寻找的,谢谢! – Razonixx
答
嗯,你的代码可以使用一些重构。
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;
}
}
我也让自己从,而(C == 0)循环提取书房计算,因为它总是计算相同的价值,但对于N次,所以你在这里获得一些optimilization。如果你可以提供更多关于你为什么预定义所有参数为0的信息,那么也许我可以在while(c == 0)循环中找到一些解决方案。如果你使用java 8,你也可以将while循环提取到另一个方法,并将一些函数作为参数。
这很好,你做的任何优化都是完全不会察觉的。尽管你可能想要清理你的标签间距。 –
当有一个ArithmeticException时,他们只能回去为'den“而不是'num'尝试另一个值。这是意图吗? – jingx
@jingx是的,这是意图,因为如果分母是0,没有理由修改分子。 – Razonixx