简体   繁体   English

投掷和捕捉异常

[英]Throwing and Catching Exceptions

Hey StackOverflow Community, 嘿StackOverflow社区,

Regarding throwing exceptions. 关于抛出异常。 Generally, when do I throw and exception, and when do I catch it? 一般来说,我什么时候抛出异常,什么时候抓住它?

Let' say I come across these situation where I have to quit because of some problem that occurred and I can't recover from it. 让我们说,由于发生了一些问题而无法从中恢复,我遇到了这种情况。 Do I throw or do I catch? 我扔了还是抓住了?

I do this right now: 我现在这样做:

    try {
       // some code
    }
    catch (IOException e) {
       logger.info("Failed to do something, and cannot continue" + e.getMessage(), e);
       e.printStackTrace();
       throw e;
    }

Is this the right thing to do? 这是正确的做法吗? Would it be more appropriate if I just threw the exception? 如果我抛出异常会更合适吗? Sorry, I'm a newbie at exceptions :) 对不起,我是异常的新手:)

You generally catch an exception in a method when you want your program to continue running. 当您希望程序继续运行时,通常会在方法中捕获异常。 You throw an exception when you want a higher level method that is calling that method to handle the exception instead. 如果需要更高级别的方法来调用该方法来处理异常,则抛出异常。 For example, you might throw it all the way back to your Main method, which has a try..catch block (likely with different catch blocks for different exceptions) encapsulating all your method calls, and exceptions can be handled there (for example by ending the program). 例如,您可以将它一直抛回Main方法,该方法具有try..catch块(可能具有针对不同异常的不同catch块)封装所有方法调用,并且可以在那里处理异常(例如,结束该计划)。

Remember that throwing an exception will end the method immediately. 请记住,抛出异常将立即结束该方法。 This affects the flow of your code. 这会影响代码的流程。 If you might have an exception in the middle of the method, and the code below it can't run if that exception happened, then you would need to either wrap the whole section in a try/catch block or throw an exception. 如果在方法中间可能有异常,并且如果发生异常,则下面的代码无法运行,那么您需要将整个部分包装在try / catch块中或抛出异常。

A general word of advice - printStackTrace() is bad. 一般的建议 - printStackTrace()很糟糕。 You can create better error output yourself (and you can include the stack trace as well with your output). 您可以自己创建更好的错误输出(并且您可以在输出中包含堆栈跟踪)。 Even better, use logging. 更好的是,使用日志记录。

I recommend reading this introduction to exceptions and this article which covers good and bad exception patterns . 我建议阅读这个例外介绍本文,其中介绍了好的和坏的异常模式

If a fatal exception occurs catch the exception and end your program nicely. 如果发生致命异常,请捕获异常并很好地结束您的程序。 Rethrowing without catching will just kill your program. 没有捕获的重新抛出只会扼杀你的程序。

If you know the exception is not something you can handle, let the exception go without catching it in the first place. 如果你知道异常不是你可以处理的东西,那么让异常在没有捕获它的情况下进行。 Have one exception handler that catches everything and logs it to a file along with the stacktrace. 有一个异常处理程序捕获所有内容并将其与stacktrace一起记录到文件中。 For instance, if your program is running from the command line you can catch everything in the main method and log it there. 例如,如果您的程序从命令行运行,您可以捕获main方法中的所有内容并将其记录在那里。


You catch an exception when you have something to do with it. 当你有什么用它做你捕捉异常。 In your case - Write to the log, display a message to the user and quit in an orderly fashion. 在您的情况下 - 写入日志,向用户显示消息并以有序的方式退出。
You throw an exception when there is nothing else you can do about it when it happens. 当它发生时,你无法做任何其他事情时抛出异常。

I recommend you to use Microsoft's enterprise library exception handling application block . 我建议您使用Microsoft的企业库异常处理应用程序块 It will help you deal with your exceptions is a way that you would be able to control the flow and make changes configurationally. 它将帮助您处理异常,这是一种您可以控制流并以配置方式进行更改的方法。

捕获后我会记录事件,然后做你需要的东西,以便整齐关闭,然后调用System.exit但我不会再次抛出该异常。

It is bad practice to have your program terminate due to an unhandled exception. 由于未处理的异常导致程序终止是不好的做法。 If you catch an exception that is fatal and unrecoverable do the following: 如果您捕获致命且不可恢复的异常,请执行以下操作:

  1. log it 记录下来
  2. perform any necissary cleanup 执行任何necissary清理
  3. terminate the program 终止程序

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

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