HiveBrain v1.2.0
Get Started
← Back to all entries
debugModerate

Making error messages more efficient

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
errorefficientmoremessagesmaking

Problem

I am using a way of making error messages, and it works fine. It is just starting to get a little annoying as my project gets larger.

I was wondering if there was a way to do with with out having to rename every part of the error, and make a code so it would pick up the function it occurred in.

Private Sub tsbEditNeighbour_Click(sender As Object, e As EventArgs) Handles tsbEditNeighbour.Click
    ' Error Checking

    On Error GoTo Err_tsbEditNeighbour_Click

    Dim CurrentRow As DataRowView = TryCast(PropertyNeighboursLoadRecordsBindingSource.Current, DataRowView)

    ' Load Warranty
    Dim uNeighbours As New frmNeighbours
    With uNeighbours
        .PropertyID = Val(lblPropertyIDValue.Text)
        .NeighbourID = Val(CurrentRow("NeighbourID"))
        .LoadNeighbours()
        .ShowDialog()
        Me.Show()
    End With

Err_tsbEditNeighbour_Click:
    If Err.Number <> 0 Then
        sErrDescription = Err.Description
        WriteAuditLogRecord(Me.Name, "tsbEditNeighbour_Click", "Error", sErrDescription)
        MsgBox("System Error occurred" & Chr(13) & "tsbEditNeighbour_Click" & Chr(13) & sErrDescription, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "AztecCRM - Error Reporting")
    End If
End Sub

Solution

There's probably much more to say about this code, but there's one point that deserves immediate and serious attention: You're handling errors the way vba and vb6 did it... but this code can only be vb.net:

Handles tsbEditNeighbour.Click


In the .NET world, you throw away all this On Error GoTo Madness, and work with Exceptions instead.

Exceptions are a much more robust way of handling errors, and give you much more information than just an error number and description (including the whole stack trace, and the exact code file and line number that threw the exception): see System.Exception on MSDN.

The basic idea is that you try some code, and catch to handle any exceptions thrown:

Try
    'some code
Catch ex As Exception
    'error-handling
End Try


See Try-Catch-Finally statements on MSDN for the full syntax.

I'll get back to this post later, to review other points.

Code Snippets

Handles tsbEditNeighbour.Click
Try
    'some code
Catch ex As Exception
    'error-handling
End Try

Context

StackExchange Code Review Q#79590, answer score: 11

Revisions (0)

No revisions yet.