[英]code not working after catching an exception
當用戶輸入的值不是整數時,我試圖捕獲異常。 輸入非整數時,將捕獲異常,但不會恢復正常。 我想要它做的是回到程序打開時的啟動方式:
private void calculateActionPerformed(java.awt.event.ActionEvent evt)
{
int quantityOfBoxes;
int width, length, height;
boolean excep;
do {
excep = false;
try {
quantityOfBoxes = Integer.parseInt(quantityField.getText());
} catch (Exception exRef) {
System.err.println(exRef);
JOptionPane.showMessageDialog(null,
"Values must be in integer",
"Error",
JOptionPane.WARNING_MESSAGE);
excep = true;
}
quantityField.setText("");
} while (excep);
}
您應該擺脫while循環。 如果引發了異常,請捕獲該異常,但不要繼續循環,因為只要quantityField.getText()
不變(實際上,您在catch塊中將其更改為“”即可,但是仍然會拋出相同的異常quantityField.getText()
異常),您將一遍又一遍地獲取此異常,並且循環永遠不會終止。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.