David Pullar
David Pullar

Reputation: 706

Try-Catch-Continue in .NET

I was wondering if a try-catch-continue construct exists with the .NET framework. Now, I understand that some may look down on exception ignoring, but I believe this to be a valid case.

I am currently doing some reporting using Telerik Reporting and there are some fields that I choose to manually override for aesthetics. The issue is, I don't care whether these columns exist or not. But, if they do, I would like the change to be made. Here's a snippet:

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

Now imagine, that I have 5+ GetHeader or GetField calls. Am I going to wrap each within it's own try-catch block (or if statement)? It would be nice to log the missing column and move on, but the execution of the code within the block is irrelevant to the overall product.

Upvotes: 3

Views: 2714

Answers (3)

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

Option 1

However, what you can do is to create a list of actions.

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)

Then you iterate the list and invoke each action inside a try/catch block.

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

Option 2

Create a method with optional nullable parameters.

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)

Upvotes: 4

Bradley Uffner
Bradley Uffner

Reputation: 17001

You could do something like this using Anonymous methods

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

Upvotes: 1

paparazzo
paparazzo

Reputation: 45106

Sorry but I don't use a lot of VB so some of this syntax might be off
Just call this 5 times with various columns

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

Upvotes: 0

Related Questions