我刚刚看到一个关于try-catch的问题,哪些人(包括Jon Skeet)认为空catch块是一个非常糟糕的主意?为什么呢?在任何情况下,空接点都不是错误的设计决策吗?

我的意思是,例如,有时你想从某个地方(webservice,数据库)获得一些额外的信息,你真的不关心你是否会得到这个信息。所以你试着获取它,如果发生了什么,没关系,我只会添加一个“catch (Exception ignored){}”,仅此而已


当前回答

在极少数情况下,这样做是合理的。在Python中,你经常会看到这样的结构:

try:
    result = foo()
except ValueError:
    result = None

所以它可能是OK的(取决于你的应用程序):

result = bar()
if result == None:
    try:
        result = foo()
    except ValueError:
        pass # Python pass is equivalent to { } in curly-brace languages
 # Now result == None if bar() returned None *and* foo() failed

在最近的一个. net项目中,我必须编写代码来枚举插件dll,以查找实现特定接口的类。相关的代码(在VB中。NET,对不起)是:

    For Each dllFile As String In dllFiles
        Try
            ' Try to load the DLL as a .NET Assembly
            Dim dll As Assembly = Assembly.LoadFile(dllFile)
            ' Loop through the classes in the DLL
            For Each cls As Type In dll.GetExportedTypes()
                ' Does this class implement the interface?
                If interfaceType.IsAssignableFrom(cls) Then

                    ' ... more code here ...

                End If
            Next
        Catch ex As Exception
            ' Unable to load the Assembly or enumerate types -- just ignore
        End Try
    Next

尽管在这种情况下,我承认在某个地方记录失败可能是一种改进。

其他回答

我的意思是,例如,有时你想从某个地方(webservice,数据库)获得一些额外的信息,你真的不关心你是否会得到这个信息。所以你试着获取它,如果发生了什么,没关系,我只会添加一个“catch (Exception ignored){}”,仅此而已

因此,以您的示例为例,在这种情况下这是一个坏主意,因为您捕获并忽略了所有异常。如果您只捕获einfofromirrelevance sourcenotavailable并忽略它,那是可以的,但您没有。您还忽略了ENetworkIsDown,它可能重要,也可能不重要。你忽略了enetworkcardhasmelting和efpuhasdecisidthatoneplusoneisseventeen,它们几乎肯定是重要的。

如果将空捕获块设置为只捕获(并忽略)您知道不重要的某些类型的异常,则空捕获块不是问题。在这种情况下,最好压制并默默地忽略所有异常,而不停下来先检查它们是否是预期的/正常的/不相关的,这种情况非常罕见。

空捕获块表示程序员不知道如何处理异常。它们正在抑制异常可能出现的冒泡,并由另一个try块正确处理。总是尝试做一些你正在捕捉的例外。

如果你不知道在catch block中要做什么,你可以只记录这个异常,但不要让它空着。

        try
        {
            string a = "125";
            int b = int.Parse(a);
        }
        catch (Exception ex)
        {
            Log.LogError(ex);
        }

在极少数情况下,这样做是合理的。在Python中,你经常会看到这样的结构:

try:
    result = foo()
except ValueError:
    result = None

所以它可能是OK的(取决于你的应用程序):

result = bar()
if result == None:
    try:
        result = foo()
    except ValueError:
        pass # Python pass is equivalent to { } in curly-brace languages
 # Now result == None if bar() returned None *and* foo() failed

在最近的一个. net项目中,我必须编写代码来枚举插件dll,以查找实现特定接口的类。相关的代码(在VB中。NET,对不起)是:

    For Each dllFile As String In dllFiles
        Try
            ' Try to load the DLL as a .NET Assembly
            Dim dll As Assembly = Assembly.LoadFile(dllFile)
            ' Loop through the classes in the DLL
            For Each cls As Type In dll.GetExportedTypes()
                ' Does this class implement the interface?
                If interfaceType.IsAssignableFrom(cls) Then

                    ' ... more code here ...

                End If
            Next
        Catch ex As Exception
            ' Unable to load the Assembly or enumerate types -- just ignore
        End Try
    Next

尽管在这种情况下,我承认在某个地方记录失败可能是一种改进。

因为如果抛出异常,您将永远不会看到它——默默地失败是最糟糕的选择——您将得到错误的行为,而且不知道它发生在哪里。至少在那里放一条日志消息!即使是“不可能发生”的事情!