Java 自定义异常无法正常工作

Java custom exception not working properly

我正在创建一个具有 3 个输入字段(小时、分钟和秒)的小计时器程序。出于某种原因,OverTwentyFourException 异常不适用于小时输入字段。这是我的部分代码:

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
                    );
                }
        }
    });

OverTwentyFourException class:

class OverTwentyFourException extends Exception {
OverTwentyFourException() {
    Gui gui = new Gui();
    JOptionPane.showMessageDialog(
            gui,
            "INPUT ERROR: The 'Hours' number needs to be lower than 24",
            "Invalid Input - Hours",
            JOptionPane.ERROR_MESSAGE
    );
}

}

如果我在小时字段中键入“25”,我会看到消息对话框,但文本不会按照我的代码设置回“00”,并且按钮停止工作。我不明白为什么分和秒字段工作得很好而且它们是相同的。我看不出 hrsChosenminsChosen / secsChosen

有什么区别

当您试图从异常本身内部处理 异常时,您遇到了一些问题,这不是应该发生的事情。相反,只需让您的自定义异常 class 扩展异常 class,或许给它一个接受字符串的构造函数,并使用相同的字符串在其中调用超类的构造函数。例如异常可能如下所示:

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 修饰符,因为您的字段绝对 而不是 应该是静态的,并标记为静态。看起来您正试图以错误的方式修复对非静态字段的访问。与其将这些字段设为静态,不如避免以任何静态方式访问它们。
  • 这看起来很奇怪:startButton.getIcon().toString()。为什么要获取图标的字符串表示形式?你打印过这个吗?这不是你想的那样,它很可能会搞乱你的程序。相反,也许您想获取 ActionEvent 的 actionCommand。