Question

So I handle all exceptions in my project within my Global.asax page. For example, on a random page I could have:

Protected Sub SomeFunction()

        Try
            'do something here

        Catch ex As Exception

            Throw ex

        End Try

    End Sub

So then the exception bubbles up to the Global.asax page where I handle it and log to the database like so:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

        Dim ex As Exception = Server.GetLastError
        Dim loggedExceptionID As Integer = 0

        Try
            loggedExceptionID = New BLL.ExceptionHandler().LogException(ex)
            Response.Redirect("~/ErrorPage.aspx?ID=" & loggedExceptionID, False)

        Catch loggingError As Exception

            'what do I do here?

        End Try

    End Sub

The BLL.ExceptionHandler().LogException(ex) function just writes the error details to the database.

So my question is in the Application_Error method, do I need the try/catch block when trying to write to the database? I'm thinking I would in case something goes wrong with the connection, etc. - but is this really necessary? Also, what would I do in the catch block if there is an error? At that point I would be catching the error of logging an error which is confusing in its own right.

Was it helpful?

Solution

It's probably a good idea to have a try/catch there, and what I would do is write the details to a text file or xml file so that you can refer back to both errors in the future and try to fix them. That of course means you would probably want another try/catch around writing to the disk, in which case I would just give up and call it a day. =)

On a completely separate note, you really should check out ELMAH: http://code.google.com/p/elmah/. This does probably everything you want and a lot more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top