为什么我不应该将每个块都包裹在“try”中 - “catch”? [英] Why should I not wrap every block in "try"-"catch"?
问题描述
我只是发布'你应该永远包装可以抛出try,catch块的调用。'到这个问题被告知这是'非常糟糕的建议' - 我想明白为什么。
一个方法只能捕捉一个异常,当它可以以一种明智的方式处理它。
否则,传递它,希望一个方法更高调用堆栈可以使它有意义。
正如其他人所指出的,最好的做法是在最高级别有一个未处理的异常处理程序调用堆栈以确保记录任何致命错误。
I have always been of the belief that if a method can throw an exception then it is reckless not to protect this call with a meaningful try block.
I just posted 'You should ALWAYS wrap calls that can throw in try, catch blocks.' to this question and was told that it was 'remarkably bad advice' - I'd like to understand why.
A method should only catch an exception when it can handle it in some sensible way.
Otherwise, pass it on up, in the hope that a method higher up the call stack can make sense of it.
As others have noted, it is good practice to have an unhandled exception handler (with logging) at the highest level of the call stack to ensure that any fatal errors are logged.
这篇关于为什么我不应该将每个块都包裹在“try”中 - “catch”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!