.NET 中的 Try-Catch-Continue

Try-Catch-Continue in .NET

我想知道 .NET 框架是否存在 try-catch-continue 结构。现在,我知道有些人可能看不起异常忽略,但我相信这是一个有效的案例。

我目前正在使用 Telerik Reporting 做一些报告,为了美观,我选择手动覆盖一些字段。问题是,我 不关心 这些列是否存在。但是,如果他们这样做,我希望做出改变。这是一个片段:

Public Overrides Sub BaseStyles()
    MyBase.BaseStyles()

    Try
        GetHeader("Employee Name").Width = Unit.Cm(3.2R)
        GetFields("Some Field").Width = Unit.Cm(2.7R)
        GetFields("Employee Name").Style.TextAlign = HorizontalAlign.Left
    Catch ex As Exception
        Trace.TraceError("Columns do not exist. Message: " + ex.Message)
        ' Continue Try
    End Try

    AddStyles(TableFieldRules(), ReportAttributes.FIELDS)
    AddStyles(TableHeaderRules(), ReportAttributes.HEADERS)
    AddStyles(FieldConditionalRule(), ReportAttributes.FIELDS)
End Sub

现在想象一下,我有 5+ 个 GetHeaderGetField 调用。我要将每个包装在它自己的 try-catch 块(或 if 语句)中吗?记录缺失的列并继续前进会很好,但是块内代码的执行与整个产品无关

抱歉,我用得不多 VB,所以有些语法可能会被取消
只需用各种列调用 5 次

Public void Sub StyleMe(string colname, Decimal w1, Decimal w2)
    Try
        GetHeader(colname).Width = Unit.Cm(w1)
        GetFields(colname).Width = Unit.Cm(w2)
        GetFields(colname).Style.TextAlign = HorizontalAlign.Left
    Catch ex As Exception
        Trace.TraceError("Columns do not exist. Message: " + ex.Message)
        ' Continue After Here
    End Try
End Sub

你可以使用匿名方法做这样的事情

Public Sub DoThings()
    IgnoreException(Sub()
                        'Any code in here will have exceptions ignored and execution will continue after this block
                        'Do things with field1
                    End Sub)

    IgnoreException(Sub()
                        'Do things with field2
                    End Sub)

    IgnoreException(Sub()
                        'Do things with field3
                    End Sub)

    IgnoreException(Sub() Console.Write(CStr(Nothing))) 'single line example
    IgnoreException(Sub() Console.WriteLine("Even though an exception was thronw by the previous line, this line still executes"))

End Sub


Private Sub IgnoreException(action As Action)
    Try
        action.Invoke()
    Catch ex As Exception
        'ignore exceptions
        'Log the exception if you want
    End Try
End Sub

There's no "try-catch-continue" statement in vb.net.

选项 1

但是,您可以做的是创建一个操作列表。

Dim actions As New Dictionary(Of String, Action(Of String))

'Single-line:

actions.Add("ColumnName1", Sub(name) GetHeader(name).Width = Unit.Cm(1.0R))

actions.Add("ColumnName2", Sub(name) GetHeader(name).Width = Unit.Cm(2.0R))

actions.Add("ColumnName3", Sub(name) GetHeader(name).Width = Unit.Cm(5.0R))

'Multi-line:

actions.Add("ColumnName4", Sub(name)
                               GetHeader(name).Width = Unit.Cm(3.0R)
                               GetFields(name).Width = Unit.Cm(8.0R)
                               GetFields(name).Style.TextAlign = HorizontalAlign.Left
                           End Sub)

然后迭代列表并在 try/catch 块中调用每个操作。

For Each item In actions
    Try
        item.Value.Invoke(item.Key)
    Catch ex As Exception
        Trace.TraceError(String.Format("The column '{0}' do not exist.", item.Key))
    End Try
Next

选项 2

创建一个带有可选可为空参数的方法。

Public Sub SetColumnStyle(name As String, Optional width As Double? = Nothing, Optional tAlign As TextAlign? = Nothing)
    Try
        If (width.HasValue) Then
            GetHeader(name).Width = width.Value
        End If
        If (tAlign.HasValue) Then
            GetFields(name).Style.TextAlign = tAlign.Value
        End If
    Catch ex As Exception
        Trace.TraceError(String.Format("The column '{0}' do not exist.", name))
    End Try
End Sub

SetColumnStyle("ColumnName1", tAlign:=HorizontalAlign.Left)