在.NET中尝试抓取 - 继续

时间:2015-02-13 17:24:56

标签: .net vb.net try-catch

我想知道.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语句)?记录缺失的列并继续前进将会很不错,但是块中代码的执行与整个产品的无关

3 个答案:

答案 0 :(得分:4)

  

vb.net中没有" try-catch-continue" 声明。

选项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)

答案 1 :(得分:1)

您可以使用匿名方法

执行此类操作
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

答案 2 :(得分:0)

很抱歉,但我没有使用很多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