Global Error Handler – VB.NET example

This little example just catches all unhandled exceptions and forwards them to a page, ErrorHandler.aspx, which then displays things a little prettier.

Global.asax.vb

Imports System.Web.SessionState

Public Class Global_asax
    Inherits System.Web.HttpApplication

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session is started
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires at the beginning of each request
    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires upon attempting to authenticate the use
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when an error occurs
        Dim ctx As HttpContext = HttpContext.Current
        Dim oException As Exception = ctx.Server.GetLastError()
        ' do not redirect for 404
        If oException.GetType().FullName = "System.Web.HttpException" Then
            Dim exHttp As HttpException = DirectCast(oException, HttpException)
            If exHttp.GetHttpCode() = 404 Then
                Throw exHttp
            End If
        End If

        ctx.Items("LastError") = oException
        ctx.Server.ClearError()
        ctx.Server.Transfer("ErrorHandler.aspx")

    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application ends
    End Sub

End Class

ErrorHandler.aspx


    <form id="form1" runat="server">
    <div>
    <asp:Panel ID="panel1" runat="server">
    <h2>An error occurred.</h2>
    <p><asp:Label ID="lblUserMsg" runat="server" /></p>
    <hr />
    <p><asp:Label ID="lblErrType" runat="server" Text="<b>Type:</b> " /></p>
    <p><asp:Label ID="lblErrMsg" runat="server" text="<b>Message:</b> " /></p>
    <p><asp:Label ID="lblErrDetail" runat="server" text="<b>Detail:</b> " /></p>
    <p><asp:Label ID="lblTrace" runat="server" Text="<b>Trace:</b> " /></p>
    <p><asp:Label ID="lblErrSrc" runat="server" Text="<b>Source:</b> " /></p>
    </asp:Panel>
    </div>
    </form>

ErrorHandler.aspx.vb


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' application_onerror redirects here; if there is any compilation error,
        ' you get an infinite loop.
        ' SO BE CAREFUL CHANGING THIS PAGE
        '
        Dim lastException, baseException As Exception
        Try
            lastException = DirectCast(Context.Items("LastError"), Exception)

            Dim detail As String = DirectCast(Context.Items("ErrorDetail"), String)
            If Not lastException Is Nothing Then
                baseException = lastException.GetBaseException()
                lblErrType.Text += baseException.GetType().ToString()
                lblErrMsg.Text += baseException.Message
                If Not String.IsNullOrEmpty(detail) Then
                    lblErrDetail.Text += detail
                End If
                lblTrace.Text += baseException.StackTrace
                lblErrSrc.Text += baseException.Source
            Else
                lblErrType.Text += "Not available"
                lblErrMsg.Text += "Not available"
                lblTrace.Text += "Not available"
                lblErrSrc.Text += "Not available"
            End If
        Catch oException As Exception
            'mostly this is here in case we did something stupid in that try block up there.
            lblErrMsg.Text = "Error Message: " + oException.Message
            lblTrace.Text = "Stack Trace: " + oException.StackTrace
            lblErrSrc.Text = "Error Source: " + oException.Source
        End Try

    End Sub

Note that this would catch all exceptions, including compile errors, so be sure to precompile before deploying! If you don’t, and there’s a compile error on the error page itself, you have fun with an endless loop. 

For more information, see How to: Handle Application-Level Errors

Tags: , ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Global Error Handler – VB.NET example”

  1. [...] is the same as the VB.NET global error handler example, but using [...]

Leave a Reply