Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 272
David Pullar Avatar asked Dec 29 '25 15:12

David Pullar


2 Answers

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)
like image 166
Bjørn-Roger Kringsjå Avatar answered Jan 01 '26 07:01

Bjørn-Roger Kringsjå


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
like image 41
Bradley Uffner Avatar answered Jan 01 '26 07:01

Bradley Uffner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!