简体   繁体   English

Java - 如何在“异常”之后停止我的程序读取下一行

[英]Java - How do I stop my program to read next line(s) after an “Exception”

I need some help. 我需要一些帮助。

When I made a "catch Exception" thing, it wont stop reading more lines. 当我做出“捕捉异常”的事情时,它不会停止阅读更多的线条。 How do I prevent this? 我该如何防止这种情况?

I did like this: 我喜欢这样:

try {
    double1 = Double.parseDouble(args[2]);
} catch (NumberFormatException e) {
    e.printStackTrace();
    user.sendMessage("Can't parse 3rd argument into a number!");
} 
if(double1 > 200 || double1 <= 0) {
user.sendMessage("Error. Nothing special here.");
}

When I get an exception for parsing error, it will continue reading to read the other error prevention method [ if (double > 200 || double 1 <= 0) {} ] and because double1 wasn't a number, it will send out to the user who perform the action another ERROR string. 当我得到解析错误的异常时,它将继续读取以读取其他错误预防方法[if(double> 200 || double 1 <= 0){}]并且因为double1不是数字,它将发送出去给执行该操作的用户另一个ERROR字符串。

This is how it looks for the user: 这是它寻找用户的方式:

Message: Can't parse 3rd argument into a number! 消息:无法将第三个参数解析为数字!

Message: Error. 消息:错误。 Nothing special here. 这里没什么特别的。

Two error messages, one isn't even supposed to be there. 两条错误消息,其中一条甚至不应该存在。 How do I make it so only the first error shows up? 我怎么做才这样只出现第一个错误? So it prevents the 2nd error message? 所以它阻止了第二条错误消息?

try {
    double1 = Double.parseDouble(args[2]);

    if (double1 > 200 || double1 <= 0) {
        user.sendMessage("Error. Nothing special here.");
    }
} catch (NumberFormatException e) {
    e.printStackTrace();
    user.sendMessage("Can't parse 3rd argument into a number!");
} 

This works, but it is not a very good design. 这有效,但它不是一个很好的设计。 The NumberFormatException is not properly handled, since you are logging it and then allowing the rest of the code to run. NumberFormatException未正确处理,因为您正在记录它,然后允许其余代码运行。 You actually want it to stop the execution and not run the rest of the code (the if condition), so that's what the exception should do - either do an early return or propagate the exception to be handled further up in the stack. 你实际上希望它停止执行而不运行其余代码( if条件),这就是异常应该做的事情 - 要么提前返回,要么传播异常以便在堆栈中进一步处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的 Java 程序在读取到某个频率后立即停止捕获音频? - How do I make my Java program stop capturing audio as soon as a certain frequency has been read? 如何在 Java 中使用 BufferedReader 读取下一行? - How do I read next line with BufferedReader in Java? 如何读取下一行 BufferedReader? - How do I read the next line BufferedReader? 在调用r.exec启动cmd提示符后,如何停止执行我的java程序 - How to stop the execution of my java program after i invoke r.exec to start a cmd prompt 在Java中,如果前一个查询的值为true,如何使我的程序仅询问下一个查询? - In Java, how can I make my program only ask the next inquiry if the previous inquiry's value is true? 在执行试图中断线程(并失败)并继续执行的Java程序后,如何停止Ant挂起? - How do I stop Ant from hanging after executing a java program that attempted to interrupt a thread (and failed) and continued? 我如何在Java中停止服务器 - How do i stop my server in java 如何让 java 程序从文件中只读取一行? - How do I make a java program read just one line from the file? 如何设置Eclipse以阻止异常发生? - How do I setup Eclipse to stop on the line an exception occurred? 如何获得程序以Java读取txt文件? - How do I get my program to read my txt file in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM