多次或单次尝试捕捉 [英] Multiple or Single Try Catch

查看:189
本文介绍了多次或单次尝试捕捉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力清理我的一些代码,我来到一个点,我不知道哪条路线会更好。

I'm working on cleaning up some of my code and I came to a point where I wasn't sure which route would be better.

目前我有一个单一的try catch块我的大部分方法,它处理一些独立的异常在结束,但我想有更多的try catch块将更好只是为了维护。然而,在分解代码时,我来到一个点,我正在为同一类型的异常写多个块。我可以看到上面为每个部分写一个块,因为我可以给更多的细节为什么它失败。

Currently I have a single try catch block over the majority of my method and it handles a few separate exceptions at the end, but I figured having more try catch blocks would be better just for maintenance. However, while breaking down the code I came to a point where I was writing multiple blocks for the same type of exception. I can see the up side to writing a block for each part since I can then give more specifics to why it failed.

我的问题是这是...有一个缺点是这样做?有没有性能问题或一些其他隐藏的怪物,我没有看到?

My question is this... is there a downside to doing this? Could there be performance issues or some other hidden monster that I'm not seeing?

另外,在一个方法中处理多个异常的首选方法是有一个行业标准?

Also, what is the preferred way of handling multiple exceptions in a method, is there an industry standard?

为了更好地说明我的观点,这里有一些伪代码。

Just to better illustrate my point here's some pseudo code

//multiple try catch for same exception
try {
     //some code here
} catch (MyException e) {
     //specific error message here
}
try {
     //some different code here
} catch (MyException e) {
     //more specific error message indicating a different issue
}


推荐答案

我总是试图降低嵌套的层次以提高可读性和可维护性。如果你有n个try / catch块,每个处理相同类型的异常,为什么不重构代码可以将异常抛出的方法...它看起来像:

I always try to reduce levels of nesting for readability and maintainability. If you have n try/catch blocks, each handling the same type of exception, why not refactor the code that can throw the exception into methods...it would look something like:

try {
    firstBatchOfTricky();
    secondBatchOfTricky();
    ....
    nthBatchOfTricky();
} catch (ItWentBoomException e) {
   // recover from boom
} catch (ItWentBangException e) {
   // recover from bang
}

这是IMHO比具有多个try / catch更易读。请注意,你的方法应该描述他们在自我记录代码的精神下做什么。

which is IMHO much more readable than having multiple try/catches. Note that your methods should describe what they do in the spirit of self documenting code.

由于你有你自己的异常类型,你可以添加你需要的数据到异常在catch块中做不同的事情。当你说'更具体的消息',你可以只是抛出异常与详细的消息;你不应该需要多个catch块。如果你想根据异常的状态做很大的不同的事情,只需创建更多的异常类型和catch块,但只有一个try块,因为我的伪代码显示...

Since you have your own Exception type, you can add the data you need to the exception to do different things in the catch block. When you say 'more specific message', you can just throw the exception with the detailed message; you shouldn't need multiple catch blocks. If you want to do drastically different things based on the state of the exception, just create more exception types and catch blocks but only one try block, as my pseudocode shows...

最后,如果你无法从异常中恢复,你不应该把代码和catch块混在一起。抛出一个运行时异常,让它冒泡。 (从@tony的意见中的好建议)

Finally, if you can't recover from the exception(s), you should not clutter the code with catch blocks. Throw a runtime exception and let it bubble. (Good advice from @tony in the comments)

这篇关于多次或单次尝试捕捉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆