?
Solved

Error Trapping in ASP.NET

Posted on 2005-03-15
4
Medium Priority
?
825 Views
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();
     ExceptionManager.Publish(objErr);
     Server.ClearError();
}

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.

Thanks.

0
Comment
Question by:fifo123
  • 2
4 Comments
 
LVL 18

Expert Comment

by:tusharashah
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"  />

-tushar
0
 

Author Comment

by:fifo123
ID: 13547508
tushar,
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?
0
 
LVL 18

Expert Comment

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

-tushar
0
 
LVL 4

Accepted Solution

by:
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.

Response.Clear()
Session.Add("ErrorIdentifier", errorIdentifier)
Server.Execute(ConfigurationSettings.AppSettings("ErrorPage"))
Server.ClearError()

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!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
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…

571 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