global.asax Application_Error not firing on forced exception??

Discussion in 'ASP .Net' started by jobs, Nov 15, 2007.

  1. jobs

    jobs Guest

    I'm forcing an error/exception in one page as follows


    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
    Dim badthing As New Exception("XXX")
    Throw badthing
    End Sub

    And attempting to set a session variable to that error in the
    global.asax file as follows:

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    Dim ex As Exception = Server.GetLastError()
    Session("LASTERROR") = ex.Message.ToString +
    ex.InnerException.Message.ToString + ex.Source.ToString
    Session("xx") = "xx"
    ' Code that runs when an unhandled error occurs
    End Sub

    in my web.config I also have customerrors turned on and redirecting to
    an errorpage.aspx page where I want to read that session variable:

    The exception happens and I am taken to my errors page, but no sign of
    the session variable. If I debug in VS.NET I notice it never makes it
    to that subroutine in the global.asax file.


    Am I missing something? Could it be that I have no application name?
    Is this an IIS thing maybe?

    Thanks for any help or information.
     
    jobs, Nov 15, 2007
    #1
    1. Advertisements

  2. re:
    !> The exception happens and I am taken to my errors page, but no sign of the session variable.
    !> Am I missing something?

    You need to call Server.ClearError() after setting your session variables.

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    Dim ex As Exception = Server.GetLastError()
    Session("LASTERROR") = ex.Message.ToString + ex.InnerException.Message.ToString + ex.Source.ToString
    Session("xx") = "xx"
    Server.ClearError()
    End Sub
     
    Juan T. Llibre, Nov 15, 2007
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.