Groups | Blog | Home
all groups > asp.net > february 2005 >

asp.net : Page_Error not working !?


Alex Nitulescu
2/8/2005 9:31:33 PM
Hi.

I read about Page_Error and Application_Error. However, I provoke an error
in a page but still Page_Error won't run. This is all the code I have left
in my page:

---------------------------------------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim txtTextBox As TextBox
txtTextBox.Text = "A"
End Sub

Sub Page_Error(ByVal sender As Object, ByVal e As EventArgs)
Response.Write("Error: ")
Response.Write("<p></p>")
Response.Write(Server.GetLastError.Message)
Server.ClearError()
End Sub
---------------------------------------------------------------------------------

I see "Object reference not set to an instance of an object" in the default
ASP.NET error page....
I have played with Debug=true/false, trace=True/False, <customErrors
mode="On/Off/RemoteOnly" />, etc, but to no avail.

Please, what am I missing here ?

Thank you, Alex.

fd123456 NO[at]SPAM hotmail.com
2/8/2005 11:25:30 PM
Hi Alex,

You're missing this in your Page_Load sub:

AddHandler Page.Error, AddressOf Page_Error

But I believe it only catches *server* errors (IIS), not *your
application* errors. That's why Server.GetLastError() is always empty
(Nothing), and your sub doesn't fire. To catch Application errors, I
think you must use either try/catch blocks, or the Application_Error
sub in Global.asax.vb...

HTH,

Michel

[quoted text, click to view]
William F. Robertson, Jr.
2/9/2005 9:12:56 AM
You will need to handle the Error event.

Try adding Handles MyBase.Error to yous Page_Error sub.

Sub Page_Error(ByVal sender As Object, ByVal e As EventArgs) Handles
MyBase.Error.

This Page.Error event is triggered when an unhandled exception occurs
anywhere during page processing. The Application.Error will fire when
outside of the scope of a page.

Try this out and let us know how it resolved out.

Here is the stub I used to test it out.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Throw New Exception("Exception Thrown")
End Sub

Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Error
Dim exception As Exception
exception = Server.GetLastError()
Response.Write(exception.Message)
Server.ClearError()
End Sub

bill


[quoted text, click to view]

Joyjit Mukherjee
2/9/2005 11:56:45 AM
Hi,

the Page_Error event is indeed firing and the handling code is also
executing. Tyr setting autoeventwireup = "false" and set breakpoints to the
Page_error handler to see whether that piece is executing or not.

regards
Joyjit

[quoted text, click to view]

Alex Nitulescu
2/9/2005 1:15:53 PM
Thank you all - problem solved ! :-)))

Alex.

[quoted text, click to view]

AddThis Social Bookmark Button