【问题标题】:Java Error Handling - Is this the best way to do this?Java 错误处理 - 这是最好的方法吗?
【发布时间】:2023-03-11 18:10:01
【问题描述】:

我正在制作的控制台程序中出现错误,我似乎无法处理其中的错误。

如果用户输入了不正确的输入类型(例如 int 所在的浮点数),他们会受到谴责,并且程序会运行(即使程序关闭,它也会以 my 的方式执行):

while(!scanner.hasNextLong()){
                System.out.println("You entered something bad..  Why do you hurt me?");
                System.out.println("*Calcumatastic dies, and I hope you feel remorseful*");
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException f) {
                    f.printStackTrace();
                }
                System.exit(0);
            }

例如,如果用户输入一个零作为不能等于零的变量(由于除零),则用户会受到谴责,但生活仍在继续,程序仍然可以运行:

while (b == 0) {
            System.out.println("Did you even read the instructions??  You cannot divide by zero!");
            System.out.println();
            System.out.println("Second Integer: ");
            b = scanner.nextLong();
        }

但是,如果用户尝试除以零,则输入错误的输入类型,程序会崩溃。我究竟做错了什么?我已经尝试过像在其他情况下那样进入 try/catch-while 循环,但它似乎没有奏效:

System.out.println("Second Integer: ");
        while(!scanner.hasNextLong()){
            System.out.println("You entered something bad..  Why do you hurt me?");
            System.out.println("*Calcumatastic dies, and I hope you feel remorseful*");
            try {
                Thread.sleep(4000);
            } catch (InterruptedException f) {
                f.printStackTrace();
            }
            System.exit(0);
        }
        b = scanner.nextLong();
        while (b == 0) {
            System.out.println("Did you even read the instructions??  You cannot divide by zero!");
            System.out.println();
            System.out.println("Second Integer: ");
            b = scanner.nextLong();
        }

【问题讨论】:

  • “崩溃”是什么意思?您是否收到异常和堆栈跟踪?

标签: java error-handling


【解决方案1】:

您的问题是您的输入验证似乎是连续的。也就是说,您检查无效输入,然后检查零值。每次有输入时,您可能都想同时执行这两项操作。

您可能希望在一个循环中检查所有错误情况。比如:

System.out.println("Second Integer: ");
// a loop wrapping your other validation checks
while(scanner.hasNext()){ // while there is input:
    while(!scanner.hasNextLong()){ // check for non-long input
        System.out.println("You entered something bad..  Why do you hurt me?");
        System.out.println("*Calcumatastic dies, and I hope you feel remorseful*");
        try {
            Thread.sleep(4000);
        } catch (InterruptedException f) {
            f.printStackTrace();
        }
        System.exit(0);
    }
    b = scanner.nextLong();
    while (b == 0) { // check for non-zero input
        System.out.println("Did you even read the instructions??  You cannot divide by zero!");
        System.out.println();
        System.out.println("Second Integer: ");
        b = scanner.nextLong();
    }
}

【讨论】:

    【解决方案2】:

    如果您尝试除以零,则会抛出 java.lang.ArithmeticException,当您尝试读取 long 并且用户输入无法解析的内容时,会抛出 InputMismatchException(可能您称之为“崩溃”)。

    发生错误是因为您在用户输入 0 时使用 b = scanner.nextLong(); 而未检查用户是否输入了有效的长值。

    这里是你的代码调整:

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
    
        long b = 0;
    
        do {
            System.out.println("Second Integer: ");
    
            b = readLong(scanner);
    
            if (b == 0) {
                System.out.println("Did you even read the instructions??  You cannot divide by zero!");
                System.out.println();
            }
        } while (b == 0);
    }
    
    private static long readLong(Scanner scanner) {
        if (!scanner.hasNextLong()) {
            System.out.println("You entered something bad..  Why do you hurt me?");
            System.out.println("*Calcumatastic dies, and I hope you feel remorseful*");
            try {
                Thread.sleep(4000);
            } catch (InterruptedException f) {
                f.printStackTrace();
            }
            System.exit(0);
        }
        return scanner.nextLong();
    }
    

    【讨论】:

    • 正确,InputMismatchException 被抛出,它不是崩溃。对不起,术语错误,我有点菜鸟。感谢您的帮助!
    • 没问题。我们是来帮忙的。不要忘记对答案投赞成票或反对票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多