嵌套的Try / Catch阻止了一个坏主意? [英] Are nested Try/Catch blocks a bad idea?

查看:109
本文介绍了嵌套的Try / Catch阻止了一个坏主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个这样的结构:

 尝试
'外部尝试代码,可以失败更通用的条件,
',我知道更少,可能无法处理

尝试
'内部尝试代码,可以失败与更具体的条件,
',我可能知道更多,并可能适当处理
Catch innerEx作为异常
'处理内部异常
结束尝试

将外部事件作为异常
'处理外部异常
结束尝试

我看到一些意见,嵌套尝试这样的阻止是不鼓励的,但我找不到任何具体原因。



这是不好的代码?如果是这样,为什么?

解决方案

在某些情况下,他们是个好主意,例如一个try / catch为整个方法,另一个在循环内,你想处理异常并继续处理集合的其余部分。



真的是唯一的理由它是如果你想跳过错误和继承的位,而不是展开堆栈和丢失上下文。在编辑器中打开多个文件就是一个很好的例子。



也就是说,异常应该是这样 - 例外。一个程序应该处理它们,但是尽量避免它们作为正常执行流程的一部分。它们在大多数语言(python是一个显着的例外)中的计算价格昂贵。



另一种可能有用的技术是捕获特定的异常类型...

 尝试
'从文件中读取的一些代码

Catch ex作为IOException
'处理文件访问问题(可能默认取决于使用情况)
捕获ex作为异常
'处理所有其他异常。
'如果你有一个处理程序进一步,只要省略这个Catch,让
'异常传播
抛出
结束尝试

正如Gooch在下面的评论中所指出的,我们还在我们的错误处理例程中使用嵌套的try / catch ... ...

 尝试
尝试
'登录数据库
Catch ex As Exception
'不做任何
结束尝试

尝试
'记录到文件
Catch ex As Exception
'不做任何
结束尝试
捕获ex As Exception
'放弃回家
结束尝试


Let's say we have a structure like so:

Try
  ' Outer try code, that can fail with more generic conditions, 
  ' that I know less about and might not be able to handle

  Try
    ' Inner try code, that can fail with more specific conditions,
    ' that I probably know more about, and are likely to handle appropriately
  Catch innerEx as Exception
    ' Handle the inner exception
  End Try

Catch outerEx as Exception
  ' Handle outer exception
End Try

I have seen some opinions that nesting Try blocks like this is discouraged, but I could not find any specific reasons.

Is this bad code? If so, why?

解决方案

There are certain circumstances where they're a good idea, e.g. one try/catch for the whole method and another inside a loop as you want to handle the exception and continue processing the rest of a collection.

Really the only reason to do it is if you want to skip the bit that errored and carry on, instead of unwinding the stack and losing context. Opening multiple files in an editor is a good example.

That said, exceptions should be just that - exceptional. A program should handle them but try to avoid them as part of normal execution flow. They're computationally expensive in most languages (python being a notable exception).

One other technique which can be useful is catching specific exception types...

Try
    'Some code to read from a file

Catch ex as IOException
    'Handle file access issues (possibly silently depending on usage)
Catch ex as Exception
    ' Handle all other exceptions.
    ' If you've got a handler further up, just omit this Catch and let the 
    ' exception propagate
    Throw
End Try

As pointed out by Gooch in the comments below, we also use nested try/catches in our error handling routines...

    Try
        Try
            'Log to database
        Catch ex As Exception
            'Do nothing
        End Try

        Try
            'Log to file
        Catch ex As Exception
            'Do nothing
        End Try
    Catch ex As Exception
        'Give up and go home
    End Try

这篇关于嵌套的Try / Catch阻止了一个坏主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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