Java自定义异常无法正常工作
我正在创建一个有3个输入字段(小时、分钟和秒)的小计时器程序。出于某种原因,OverTwentyFourException异常不适用于小时输入字段。以下是我的部分代码:Java自定义异常无法正常工作,java,exception,custom-exceptions,Java,Exception,Custom Exceptions,我正在创建一个有3个输入字段(小时、分钟和秒)的小计时器程序。出于某种原因,OverTwentyFourException异常不适用于小时输入字段。以下是我的部分代码: static JTextField userInputHrs; static JTextField userInputMins; static JTextField userInputSecs; static int hrsChosen; static int minsChosen; static int secsChosen;
static JTextField userInputHrs;
static JTextField userInputMins;
static JTextField userInputSecs;
static int hrsChosen;
static int minsChosen;
static int secsChosen;
startButton.addActionListener(e -> {
switch(startButton.getIcon().toString()) {
case "Pause":
timer.TimerFunctionality.pause();
break;
case "Resume":
timer.TimerFunctionality.resume();
break;
default:
try {
//Calculate seconds from user input
hrsChosen = Integer.parseInt(userInputHrs.getText());
minsChosen = Integer.parseInt(userInputMins.getText());
secsChosen = Integer.parseInt(userInputSecs.getText());
secsRemaining = hrsChosen * 3600 + minsChosen * 60 + secsChosen;
if(hrsChosen < 0 || minsChosen < 0 || secsChosen < 0)
throw new NegativeException();
if(hrsChosen > 24)
throw new OverTwentyFourException();
//Getter for two thirds of userInput for color change
twoThirdsInput = 66.66 * secsRemaining / 100;
//Getter for one third of userInput for color change
oneThirdInput = 33.33 * secsRemaining / 100;
timer.TimerFunctionality.start();
}
catch(NegativeException ee) {
userInputHrs.setText("00");
userInputMins.setText("00");
userInputSecs.setText("00");
}
catch(OverTwentyFourException ee) {
userInputHrs.setText("00");
}
catch(NumberFormatException ee) {
userInputHrs.setText("00");
userInputMins.setText("00");
userInputSecs.setText("00");
JOptionPane.showMessageDialog(
Gui.this, "INPUT ERROR: Please use digits",
"Invalid Input",
JOptionPane.ERROR_MESSAGE
);
}
}
});
}
如果我在“小时”字段中键入“25”,我将获得消息对话框,但文本不会按照我的代码设置回“00”,按钮将停止工作。我不明白为什么分秒字段工作得很好,而且它们是理想的。我看不出hrsChosen和minschoselected/secsChosen之间有什么区别。当您试图从异常本身内部处理异常时,这里的情况是倒退的,这不是应该发生的。相反,只需让您的自定义异常类扩展该异常类,也许给它一个接受字符串的构造函数,并使用相同的字符串调用其中的super构造函数。e、 g.例外情况可能如下所示:
public class OverTwentyFourException extends Exception {
// allow it to handle exceptions with or without a message String
// the constructor below will call the super's default constructor
public OverTwentyFourException() {}
public OverTwentyFourException(String message) {
super(message);
}
}
<>您可以考虑扩展其他异常构造函数。
JOptionPane代码应该只在应该处理异常的其他地方的代码中,即抛出异常的地方
catch(OverTwentyFourException ee) {
// **** JOptionPane code goes here ****
userInputHrs.setText("00");
}
请注意,您的代码也有点不寻常,因为您在同一个方法中抛出和捕获同一个异常。你这样做似乎没有多大收获
无关问题:
- 您似乎错误地使用了
修饰符,因为您的字段肯定不是静态的,并标记为静态。看起来您试图以错误的方式修复对非静态字段的访问。不要将这些字段设置为静态字段,而是希望避免以任何静态方式访问它们李>static
- 这看起来很奇怪:
。为什么要获取图标的字符串表示形式?你把这个印出来了吗?它不是你想象的那样,而且很可能会把你的计划搞砸。相反,您可能希望获取ActionEvent的actionCommandstartButton.getIcon().toString()
toString()
,因为它不太可能显示你认为它显示的内容。我会直截了当地说:你不应该这样做。如果您需要比较图标,请自行比较图标。至于修复静态问题,这是基本的Java OOPs——如何创建和使用类。你应该已经知道了,我打赌。
catch(OverTwentyFourException ee) {
// **** JOptionPane code goes here ****
userInputHrs.setText("00");
}