我的代码中有关于抛出新异常的问题。
在其中一个 JTextField 中,我输入了数字 0,但它不正确,此时单击“ENTER”按钮后,会抛出新的异常消息:
throw new Exception("Wrong number of express lanes range beginnig!! \n Please enter positive number!");
该框消息出现错误消息我只能单击“确定”。单击“确定”后,我希望程序停止,因为我想更正 JTextField 中的错误数据。我不能这样做,因为在我单击“确定”后,程序继续处理。我怎样才能停止程序并更正数据?这是我看到有问题的代码的重要部分。
public class UILayer
{
private JTextField text1, text2, text3, text4, text5, text6;
private void validateNumbers() throws Exception
{
if(!text1.getText().equals("") &&
Integer.parseInt(text1.getText()) <= 0 )
throw new Exception("Wrong number of lanes!! \n Please enter positive number!");
if(!text2.getText().equals("") &&
Integer.parseInt(text2.getText()) <= 0 )
throw new Exception("Wrong number of express lanes!! \n Please enter positive number!");
if(!text3.getText().equals("") &&
Integer.parseInt(text3.getText()) <= 0 )
throw new Exception("Wrong number of express lanes range beginnig!! \n Please enter positive number!");
if(!text4.getText().equals("") &&
Integer.parseInt(text4.getText()) <= 0 )
throw new Exception("Wrong number of express lanes range ending!! \n Please enter positive number!");
if(!text5.getText().equals("") &&
Integer.parseInt(text5.getText()) <= 0 )
throw new Exception("Wrong number of customers!! \n Please enter positive number!");
if(!text6.getText().equals("") &&
Integer.parseInt(text6.getText()) <= 0 )
throw new Exception("Wrong number of max items!! \n Please enter positive number!");
}
protected class EnterClickListener implements ActionListener
{
private SimulationConfig info;
EnterClickListener( )
{
info = new SimulationConfig();
}
@Override
public void actionPerformed( ActionEvent event )
{
try
{
if(!(event.getActionCommand().equals(null )))
{
validateForm();
validateNumbers();
}
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog( null, "Error.\nPlease enter number.",
"Message", JOptionPane.INFORMATION_MESSAGE );
}
catch (Exception e)
{
JOptionPane.showMessageDialog( null, "Error.\n" + e.getMessage() ,
"Message", JOptionPane.INFORMATION_MESSAGE );
}
info.setCustomerRestriction(Integer.parseInt(text5.getText()), Integer.parseInt(text6.getText()));
info.setExpressRange(Integer.parseInt(text3.getText()), Integer.parseInt(text4.getText()));
info.setLanesNum(Integer.parseInt(text1.getText()), Integer.parseInt(text2.getText()));
showProgressBar();
task = blayer.startSimulation(info);
task.addPropertyChangeListener(new PropertyChangeListener());
task.execute();
}
}