我的代码中有关于抛出新异常的问题。
在其中一个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();
}
}
答案 0 :(得分:2)
把你的代码
info.setCustomerRestriction(Integer.parseInt(text5.getText())....
......
task.execute();
内部尝试,之后如果。像这样:
@Override
public void actionPerformed( ActionEvent event )
{
try
{
if(!(event.getActionCommand().equals(null )))
{
validateForm();
validateNumbers();
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();
}
}
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 );
}
}
答案 1 :(得分:1)
PC提供的解决方案是一个更好的替代方案就是编写一个用户定义的Exception类,它能够中止进一步的程序执行,因为你可以重用它,因此不需要再次编写相同的代码(如果你将在多个地方或java类中处理相同的异常)。 你需要做的是:
现在,您可以在代码中的任何位置重用此Exception类。