Page_Error not working !?

A

Alex Nitulescu

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

Joyjit Mukherjee

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
 
F

fd123456

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
 
W

William F. Robertson, Jr.

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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top