简体   繁体   English

有没有一种方法可以将IOException抛出为可运行状态?

[英]Is there a way to throw IOException in a runnable?

I have some code thats not working(which is a common occurrence for me), but because I am not getting an errors it just continues to run with bad data. 我有一些无法正常工作的代码(这对我来说是很常见的事情),但是因为我没有收到错误,所以它只会继续以错误的数据运行。 The problem I think is it keeps telling me to wrap parts of my code in try/catch blocks(my tests are basic, I just output a message in the try area and if it gets outputted I assume all went well. All does not seem well in my code). 我认为的问题是,它不断告诉我将部分代码包装在try / catch块中(我的测试是基本的,我只是在try区域中输出一条消息,如果输出了,我认为一切都很好。好在我的代码中)。 I understand in production, putting a try/catch statement helps the code to continue to run but its making me troubleshooting difficult because I'm trying to troubleshoot that section of my code. 据我了解,在生产环境中,放置try / catch语句有助于代码继续运行,但是这使我难以进行故障排除,因为我正尝试对代码的这一部分进行故障排除。

Is there a way around this so I can actually see when something fails within the try area? 有没有办法解决这个问题,这样我就可以真正看到尝试区域中何时出现故障?

Here's a sample of my code: 这是我的代码示例:

    try {
        ByteArrayInputStream baos_back = new ByteArrayInputStream(message);
        ObjectInputStream oos_back = new ObjectInputStream(baos_back);
        i = oos_back.readInt();
        d = oos_back.readDouble();
        list_of_ints = (int[]) oos_back.readObject();
        oos_back.reset();
        baos_back.reset();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Are you trying to get your program to blow up when this error occurs? 您是否正在尝试使程序在发生此错误时崩溃? If so, you should be able to wrap your IOException in a RuntimeException and throw that instead. 如果是这样,您应该能够将IOException包装在RuntimeException然后抛出该IOException They're unchecked, so you don't need to declare them and it should kill your program just fine. 它们未经检查,因此您无需声明它们,它应该可以杀死您的程序。

If you want your code to throw the appropriate exception, I'd suggest not using try-catch blocks at all. 如果您想让代码引发适当的异常,建议不要使用try-catch块。 Try-catch is used to handle exceptions as they arise and then keep running the program, but it sounds like you don't want to handle them at all. Try-catch用于处理异常发生时的异常,然后继续运行程序,但是听起来您根本不想处理它们。

If you do want to use try-catch blocks you could always manually throw a RuntimeException at the end of the catch block. 如果确实要使用try-catch块,则始终可以在catch块的末尾手动抛出RuntimeException。 Something like: 就像是:

throw new IOException();
try {
    // Some code...
} catch(Exception e) {
    // Error handling code...
    throw new RuntimeException(e.getMessage());
}

As I mentioned in my comment, you can catch all exceptions in Java with a blanket catch statement: 正如我在评论中提到的那样,您可以使用一揽子catch语句捕获Java中的所有异常:

try {
    // code
} catch (Exception e) {
    e.printStackTrace();
}

This will catch every Exception thrown in the try block, and the only things it won't catch are Error s. 这将捕获在try块中引发的每个Exception ,并且唯一不能捕获的是Error

In practice, you will want to limit the types of exceptions you catch, and catch more specific exceptions, so you can exception chain as follows: 在实践中,您将希望限制捕获的异常类型,并捕获更具体的异常,因此可以按以下方式进行异常链:

try {
    // code
} catch (IOException ioe) {
    // we expected this
    ioe.printStackTrace();
} catch (SomeOtherException soe) { // just an example...
    soe.printStackTrace();
} catch (Exception e) {
    // Did we expect this? Maybe not!
    e.printStackTrace();
}

The above also makes it known that you expect some types of exceptions to occur, and then a big blanket catch-all statement that might catch things you didn't expect. 上面的内容还表明,您预计会发生某些类型的异常,然后发出大范围的包罗万象的语句,可能会捕获您未曾想到的事情。

You can also log exceptions to a file or something else, rather than output them to standard out as this code does right now. 您也可以将异常记录到文件或其他内容中,而不是像现在的代码那样将它们输出到标准输出中。 A basic logging utility is java.util.logging . 基本的日志记录实用程序是java.util.logging


I still recommend learning to use a debugger though. 我仍然建议您学习使用调试器。 Debuggers can do a lot of things like halt program execution whenever an exception is thrown, and allow you to inspect the values of variables and fields at any point in the program's execution. 调试器可以执行很多操作,例如每当引发异常时就暂停程序执行,并允许您在程序执行的任何时候检查变量和字段的值。 If you use Eclipse or Netbeans or IntelliJ or other IDEs, they have debuggers. 如果使用Eclipse或Netbeans或IntelliJ或其他IDE,则它们具有调试器。 If you use the command line, there is the jdb command-line java debugger. 如果使用命令行,则有jdb命令行Java调试器。

I suggest editing your code generation template to do this 我建议编辑您的代码生成模板以执行此操作

catch ( $ExceptionClass e )
{
  // TODO: Autogenerated catch block
  e.printStackTrace();
  throw new RuntimeExcepton( e );
}

This way you have a TODO reminder, a barf on stdout, and are ensured that your program will blow up if you do not provide correct exception handler. 这样,您就有了一个TODO提醒,在stdout上有一个barf,并确保如果您没有提供正确的异常处理程序,您的程序将崩溃。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM