Passing Exception details in Session to Custom Error page.

In my recent post ASP.NET Exception Handling links  I wondered why in the article Rich Custom Error Handling with ASP.NET    Session is not considered to pass Exception details between global.asax: Application_Error  and Rich Custom Error Pages with Redirect method.
The article compare different state possibilities : Application, Context, Cookies, and QueryString.

The application that I am working with used Cache(which technically works as the same way as Application) with SessionID as a key and Response.Redirect()  to open a custom error page. The problem with Cache( and with Application) state is that it doesn't work in a web farm  environment, because Exception obeject saved on one farm server is not available after Redirect on another farm server(unless farm support session affinity). So if you have 4 web farm servers, only one of 4 errors are shown in  the custom error page(which is not good). And the problem is not noticable on development machines(which is very bad).
OK, but if SessionID was used as a key in both Application_Error  and custom error page, why not to use Session to pass Exception details. It didn't work for me initially. Post Don't redirect after setting a Session variable (or do it right)  describes that it is required to use  Response.Redirect overload with endResponse =false. However even after this I wasn't able to retrieve in custom error page object, that was saved to Session in Application_Error .
The problem is that after Application_Error  function finished, ASP.NET still considered Exception as not handled, and handles it by logging "Unhandled Exception" to the Application Event Log and interrupting normal event life cycle, which prevents to save the latest Session state changes. 
The fix is simple: if you handle Exception in Application_Error  , don't forget to call Server.ClearError();  

By the way, this is important difference between catch block and Application_Error  handler.
In catch block If you do NOT want to "swallow" exception , you should explicitely specify throw.
In Application_Error , if you want to "swallow" exception, you need to call Server.ClearError();  

To summarize below is the code, that I am using to pass Exception info from Application_Error to  custom error page using Session(actually it is the almost the same code as posted by  kurtsune in comments on Bertrand Le Roy's post .


void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
HttpContext.Current.Session["Error"] = ex.Message;
Response.Redirect("error.aspx",false);
Server.ClearError();  
}

posted @ Friday, July 20, 2007 12:22 AM
Print

Comments on this entry:

# re: Passing Exception details in Session to Custom Error page.

Left by Gersy at 3/10/2008 4:23 AM
Gravatar
Well, this is great
but how to access the current page object from Application_Error????
please reply to my E-mail if you want
thanks a lot

# re: Passing Exception details in Session to Custom Error page.

Left by rafek at 2/15/2010 4:11 AM
Gravatar
I was struggling with the same problem. Now, everything is clear, thanks to you. Regards!

# re: Passing Exception details in Session to Custom Error page.

Left by AhmedIG at 3/3/2010 4:57 AM
Gravatar
Nice info. I had a similar problem and thought that there is no way to fix that.
Thanks...

# re: Passing Exception details in Session to Custom Error page.

Left by PiedPiper at 8/15/2011 2:00 PM
Gravatar
Perfect! It helped to know ClearError HAS to be called in Application_Error as my custom error page was going awary otherwise.

# re: Passing Exception details in Session to Custom Error page.

Left by Simone at 1/15/2013 8:55 PM
Gravatar
thanks for you explanation. I surfed for 30 minutes in the web first to read your solution. In works fine.

# re: Passing Exception details in Session to Custom Error page.

Left by Xuan Khang at 2/27/2014 2:26 PM
Gravatar
I had the same issue. Thank you for your explanation. :)

Your comment:



(not displayed)

 
 
 
 
 

Live Comment Preview:

 
«September»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011