global.asax Application_Error not firing on forced exception??

J

jobs

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.
 
J

Juan T. Llibre

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top