Error Handleing at the Application Level Server Error

D

David Lozzi

Howdy,

I found a nice little book called ASP.NET 2.0 Cookbook by Michael A Kittel
and Geoffrey LeBlond. Anyway, they have some instructions on how to setup
application level error handling. Most of my functions have try..catch to
email me about an error, then I want the application level to fire off to
send the user to a custom page AND log it in the app log. When an error
occurs on my page, i get a server error

Server Error in '/newcart' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code. Exception Details:
System.NullReferenceException: Object reference not set to an instance of an
object.Source Error: Line 4:
Line 5: Protected Sub Page_Error(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Error
Line 6: Throw Server.GetLastError()
Line 7: End Sub
Line 8: Source File: c:\inetpub\wwwroot\newcart\addtobag.aspx.vb Line:
6 Any ideas?Also, I'm under the impression that if an actual error occurs,
it will not send me to the error page as specified b/c I'm on my development
machine. is that correct?Thanks!!-- David
Lozzidlozzi@(remove)delphi-ts.comwww.delphi-ts.com
 
G

Guest

David,
You shouldn't have to rethrow the sever last error in your page_error
event. As long as you don't call Server.ClearError() the Application_Error
event should still fire and redirection to the error page should occur as
defined in your web.config.

Incidentally, I normally use the Page_Error event to perform page state
clean up and the Application_Error event in the global.asax for custom error
handling (such as calling an EntLib exception policy - which could you caould
configure to do your emailing and logging functions).

Depending on the deployment I sometimes use a custom HTTPModule for hooking
in to application events. If I want to change how I'm handling an
application error then I cna change and redeploy th HTTPModule dll rather
than playing with the global.asax.

Hope this helps,
Garth
 
D

David Lozzi

The book states to "rethrow the last exception that occured...we do this
step to avoid having the exception information wrapped with an
HttpUnhandledException exception. ASP.NET automatically creates a new
HttpUnhandledException at the page level unless you rethrow the last
exception." sooo.....
 
G

Guest

David,
I ran a quick test and had no problem throwing the Server.GetLastError
within the Page_Error, redirection to the error page occured as expected.
I'll ask a question, and I hope I don't offend ;), have you set the mode of
your customErrors section to 'On'?
eg:
<customErrors defaultRedirect="~/Error.aspx" mode="On">
</customErrors>

If the mode is Off or RemoteOnly on your development machine you'll get the
standard server error.

As an aside, I don't normally mind the exceptions being wrapped in an
HttpUnhandledException. You can get the original exception from the
InnerException property of the unhandled exception in the application_error
event. I guess a down side (or up side depending on how you look at it) to
this is that HttpUnhandledExceptions are logged in the Application log on the
server.

Garth.
 
D

David Lozzi

I take great offense to the thickness of my own skull! Thank you, I'll try
that soon.
 

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