Error Trapping in ASP.NET

Posted on 2005-03-15
Medium Priority
Last Modified: 2013-11-25
Hey guys,
I am creating a ASP.NET application.  I want to log any errors that occur into the event log.  I am using the Microsoft.ApplicationBlocks.ExceptionManagement component for accomplishing that.  When the error occurs, I also want to redirect to custom error page.

Here is what I did:

In the global.asax
protected void Application_Error(Object sender, EventArgs e)
     Exception objErr = Server.GetLastError().GetBaseException();

In the web.config
<customErrors mode="Off" defaultRedirect="ErrorPage.aspx"  />

In the Page_Load event, I throw an error
private void Page_Load(object sender, System.EventArgs e)
   Throw an error...

When I execute this, the error message gets logged in the eventlog, but the page never gets redirected to the Error page.  I can't figure out what's going on here.

Also, should I avoid using try catch blocks throughout my code and let the Application_Error event catch it?  I'm not sure if there's a rule of thumb when it comes to error handling in ASP .NET.


Question by:fifo123
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 18

Expert Comment

ID: 13547429
You need customErrors mode either "On" or "RemoteOnly"

"On" Always display custom (friendly) messages.
"Off" Always display detailed ASP.NET error information. (not your page)
"RemoteOnly" Display custom (friendly) messages only to users not running

Modify it like following:
<customErrors mode="On" defaultRedirect="ErrorPage.aspx"  />


Author Comment

ID: 13547508
That worked but only when I took out the line of code:    Server.ClearError();

So, throughout my ASP .NET application, I have several utility function that perform some business rule logic.  Should I avoid putting try catch blocks?  Or should I put the try catch for each function and inside of catch do a response.redirect?
LVL 18

Expert Comment

ID: 13547546
What you can do is put  this line: Server.ClearError();  inside Page_Load event of ErrorPage.aspx...


Accepted Solution

imperial_p79 earned 400 total points
ID: 13612572
Dont have try-catch inside any of your application code. Let the exception bubble up to application error event.

Inside Application_Error have a try{} Finally{} block and in the finally block have the following code.

Session.Add("ErrorIdentifier", errorIdentifier)

Where you have the name of the error page in web.config under ErrorPage key.

And set

<customErrors mode="RemoteOnly" defaultRedirect="/common/Error.aspx" />

Hope this helps!

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question