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

Dan*_*iel 2 java exception custom-exceptions

我正在创建一个带有3个输入字段(小时,分钟和秒)的小Timer程序。由于某些原因,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
                    );
                }
        }
    });
Run Code Online (Sandbox Code Playgroud)

OverTwentyFourException类:

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
    );
}
Run Code Online (Sandbox Code Playgroud)

}

如果我在小时字段中输入“ 25”,则会出现消息对话框,但根据我的代码文本不会重新设置为“ 00”,并且按钮将停止工作。我不明白为什么分钟和秒字段可以完美工作并且它们无用。我没有看到hrsChosenminsChosen / secsChosen之间有什么区别

Hov*_*els 5

当您尝试从异常本身内部处理异常时,这里的情况倒退了,这不是应该发生的事情。而是简单地让您的自定义异常类扩展Exception类,也许给它一个接受字符串的构造函数,然后在其中使用相同的String调用父级的构造函数。例如,异常可能看起来像这样:

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);
    }
} 
Run Code Online (Sandbox Code Playgroud)

您可以考虑扩展其他异常构造函数。

JOptionPane代码仅应在应该处理异常的其他地方的代码中(在该代码中引发异常)。

catch(OverTwentyFourException ee) {
    // **** JOptionPane code goes here ****
    userInputHrs.setText("00");
}
Run Code Online (Sandbox Code Playgroud)

请注意,您的代码也有一些不寻常之处,因为您是在同一方法内引发并捕获此相同的异常。您这样做似乎并没有收获太多。

不相关的问题:

  • 您似乎误用了static修饰符,因为您已将字段标识为绝对应该是静态的。看来您正在尝试以错误的方式修复对非静态字段的访问。除了将这些字段设为静态之外,您将希望避免以任何静态方式访问它们。
  • 这看起来很奇怪:startButton.getIcon().toString()。为什么要获取图标的字符串表示形式?你打印了吗?这不是您想的那样,可能会弄乱您的程序。相反,也许您想获取ActionEvent的actionCommand。