Tuesday, September 22, 2009

Pet Peeve with Exceptions

Private Sub MyMethod()
    Try
        '...do something
    Catch ex As Exception
        Throw New Exception("MyMethod", ex)
    End Try
End Sub
The code above is a big pet peave of mine because with code like the above you end up having to dig down through multiple InnerException objects to find the right exception message.

Working with exceptions is a very easy task. Only catch an exception if you can do something about it. Re-thowing the exception is not doing something about it. Reporting the error via email, event viewer, etc., is also not doing something about the exception. All of this can be done at the form/view/page level of the application.

For instance, lets say your building a class:
Public Class Person
    Implements IDisposable

    Private m_FirstName As String = String.Empty
    Private m_LastName As String = String.Empty

    Public Property FirstName() As String
        Get
            Return m_FirstName
        End Get
        Set(ByVal value As String)
            m_FirstName = value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return m_LastName
        End Get
        Set(ByVal value As String)
            m_LastName= value
        End Set
    End Property

    Public Sub New()
    End Sub

   Public Function GetAddressByNameTheWrongWay(ByVal FirstName As String, ByVal LastName As String) As String
        Try
            '...do something and return
        Catch ex as Exception
            Throw New Exception("some dumb message here",ex)
        End Try
    End Sub

   Public Function GetAddressByNameTheRightWay(ByVal FirstName As String, ByVal LastName As String) As String
        '...do something and return
    End Sub
End Class

So now with that class we have two ways of capturing the exception in our page.

Partial Class Maintenance_IVANSAgentApplications
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim objPerson As New Person
        'Lets call it the wrong way first.
        Response.Write(objPerson.GetAddressByNameTheWrongWay(txtFirstName.text, txtLastName.text))
        'Now lets call it the correct way.
        Try
            Response.Write(objPerson.GetAddressByNameTheWrongWay(txtFirstName.text, txtLastName.text))
        Catch ex as Exception
            Response.Write(ex.Message)
        End Try
    End Sub
End Class

Since we were not able to actually do anything about the error message then we let it bubble up to our page level and catch it there.

Something that you may have been able to catch in our Class Person, would be a database connection issue. Perhaps the database was not available we could put code in to try a different database or to keep trying for so many tries. Just make sure you limit the re-tries, so that you do not get a timeout on your page while waiting for a database to return data when it will never come back.

Stanley

No comments:

Post a Comment