java 抛出和捕获异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11853348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Throwing and Catching Exceptions
提问by tomato
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 :)
这是正确的做法吗?如果我只是抛出异常会更合适吗?对不起,我是例外的新手:)
回答by smcg
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. You can create better error output yourself (and you can include the stack trace as well with your output). Even better, use logging.
一般建议 - printStackTrace() 不好。您可以自己创建更好的错误输出(并且您可以在输出中包含堆栈跟踪)。更好的是,使用日志记录。
I recommend reading this introduction to exceptionsand this article which covers good and bad exception patterns.
我推荐阅读这篇异常介绍和这篇涵盖好的和坏的异常模式的文章。
回答by juergen d
If a fatal exception occurs catch the exception and end your program nicely. Rethrowing without catching will just kill your program.
如果发生致命异常,则捕获异常并很好地结束您的程序。重新抛出而不捕获只会杀死您的程序。
回答by Nathan Hughes
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. For instance, if your program is running from the command line you can catch everything in the main method and log it there.
如果您知道该异常不是您可以处理的,那么首先不要捕获它,就让该异常消失。有一个异常处理程序可以捕获所有内容并将其与堆栈跟踪一起记录到文件中。例如,如果您的程序从命令行运行,您可以捕获 main 方法中的所有内容并将其记录在那里。
回答by Jony Adamit
You catchan 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 throwan 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. It will help you deal with your exceptions is a way that you would be able to control the flow and make changes configurationally.
我推荐你使用微软的企业库异常处理应用程序块。它将帮助您处理异常,这是您能够控制流程并以配置方式进行更改的一种方式。
回答by Zoop
After catching i would log the incident and then do what you need in order to shutdown tidily and then call System.exit but i wouldn't throw that exception again.
捕获后,我会记录该事件,然后执行您需要的操作以便整齐地关闭,然后调用 System.exit,但我不会再次抛出该异常。
回答by Colin D
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:
由于未处理的异常而终止程序是不好的做法。如果您捕获到致命且不可恢复的异常,请执行以下操作:
- log it
- perform any necissary cleanup
- terminate the program
- 记录它
- 执行任何必要的清理
- 终止程序