Java异常没有代码进行
我正在尝试制作一个程序,允许用户输入2个整数(标记)。 如果用户没有输入整数,我将创建一个try-and-catch代码 问题是,在我尝试输入字母而不是数字后,出现了一个错误,但程序继续,说我没有通过。在告诉用户他输入了错误的标记后,如何让程序停止 这是我的密码:Java异常没有代码进行,java,exception,Java,Exception,我正在尝试制作一个程序,允许用户输入2个整数(标记)。 如果用户没有输入整数,我将创建一个try-and-catch代码 问题是,在我尝试输入字母而不是数字后,出现了一个错误,但程序继续,说我没有通过。在告诉用户他输入了错误的标记后,如何让程序停止 这是我的密码: public void actionPerformed(ActionEvent e) try{ myCalculator.setCWK(Integer.parseInt(courseEnter));
public void actionPerformed(ActionEvent e)
try{
myCalculator.setCWK(Integer.parseInt(courseEnter));
myCalculator.setExam(Integer.parseInt(examEnter));
}
catch (Exception a){
System.out.print("System error");
}
displayArea.setText("" + myCalculator.calculateModuleMark());
if(myCalculator.hasPasssed()==true)
{
displayArea.setText(myCalculator.getModuleCode() + "Congratulations! You have PASSED! With a score of " + myCalculator.calculateModuleMark() + "%");
getContentPane().setBackground(Color.green);
}
else
{
displayArea.setText("I am sorry");
getContentPane().setBackground(Color.red);
}
}
打印出错误后,只需键入
return打印出错误后,只需键入return如果希望程序在某个语句后“停止”,则退出(0)
是您的朋友。所以,在你的捕获声明中,你可以
catch (Exception a){
System.out.print("System error");
System.exit(0);
}
请注意,这与返回不同,因为退出系统(0)
将完全停止程序流,而不仅仅是此特定方法。如果您希望程序在某个语句后“停止”,则退出系统(0)是您的朋友。所以,在你的捕获声明中,你可以
catch (Exception a){
System.out.print("System error");
System.exit(0);
}
请注意,这与返回不同,因为退出(0)将完全停止程序流,而不仅仅是此特定方法。
try…catch
块意味着
如果try
指令块中出现错误,请执行catch
指令块
在您的情况下,您没有退出
catch
块中的函数,因此它将继续执行。try…catch
块的意思是
如果try
指令块中出现错误,请执行catch
指令块
在您的情况下,您没有退出
catch
块中的函数,因此它将继续执行。异常处理基本上是为了防止代码在没有任何错误消息的情况下突然退出
您可以在System.out.print(“系统错误”)
之后调用System.exit(1)
注:System.exit(0)
表示程序按预期终止,而括号内的任何其他错误代码表示存在错误
那么,现在您的代码将是:
public void actionPerformed(ActionEvent e){
try{
myCalculator.setCWK(Integer.parseInt(courseEnter));
myCalculator.setExam(Integer.parseInt(examEnter));
}
catch (Exception a){
System.out.print("System error");
System.exit(1);
}
displayArea.setText("" + myCalculator.calculateModuleMark());
if(myCalculator.hasPasssed()==true)
{
displayArea.setText(myCalculator.getModuleCode() + "Congratulations! You have PASSED! With a score of " + myCalculator.calculateModuleMark() + "%");
getContentPane().setBackground(Color.green);
}
else
{
displayArea.setText("I am sorry");
getContentPane().setBackground(Color.red);
}
}
异常处理本质上是为了防止代码在没有任何错误消息的情况下突然退出 您可以在
System.out.print(“系统错误”)
之后调用System.exit(1)
注:System.exit(0)
表示程序按预期终止,而括号内的任何其他错误代码表示存在错误
那么,现在您的代码将是:
public void actionPerformed(ActionEvent e){
try{
myCalculator.setCWK(Integer.parseInt(courseEnter));
myCalculator.setExam(Integer.parseInt(examEnter));
}
catch (Exception a){
System.out.print("System error");
System.exit(1);
}
displayArea.setText("" + myCalculator.calculateModuleMark());
if(myCalculator.hasPasssed()==true)
{
displayArea.setText(myCalculator.getModuleCode() + "Congratulations! You have PASSED! With a score of " + myCalculator.calculateModuleMark() + "%");
getContentPane().setBackground(Color.green);
}
else
{
displayArea.setText("I am sorry");
getContentPane().setBackground(Color.red);
}
}