Last post Nov 18, 2009 11:13 PM by DayAndNightCoder
Jan 16, 2009 04:00 AM|DanielC.|LINK
I have an "Error Handling Module" for porcessing errors but I also have some code executed in Global.asax.
The "Error Handling Module" is exectued first (which is fine) in it I call "Server.ClearError()" because I would like to stop the execution of other error handlers.
I found an unexpected behaviour: the global.asax function Application_Error is executed!!!!
Since I called "Server.ClearError()" I was expected to stop exception bubbling.
Could somebody help me?
ASP.net 2.0 pipeline
http Handlers and modules
Jan 16, 2009 04:41 AM|MarkWells|LINK
Don't use the Server.ClearError(), just catch the error in a Try Catch block. You might want to try this technique on all of your database calls
Jan 16, 2009 05:33 AM|DanielC.|LINK
I appreciate your comment but you did not understand what the real problem is.
Jan 22, 2009 01:17 AM|Momo_Stev|LINK
Server.ClearError() just clears the previous exception. And it can't provent the exception bubbling.
Jan 22, 2009 03:51 AM|DanielC.|LINK
Thanks for your post.
Do you know how I can stop the exception bubbling?
I tried with folowing line of codes, but it does not work.
Do somebody have an idea how to do it?
Nov 16, 2009 03:13 PM|chrismasnter|LINK
I have a similar situation. The app I am working on uses the Global.asax file for exception handling. I am creating a new piece of the the application that I would like to handle all errors via the pages Error event. I need to keep the Global.asax file
from picking up the exceptions. How can this be done?
Nov 17, 2009 07:32 AM|DanielC.|LINK
After 2 years I still don't have a solution. Sorry for it.
A a workaround you might be can add to the HttpContext.Items property a value, which tells that the exception shouldn' t be handled anymore. In global.asax you look if the context contains the value and based on this information you write your logic.
I don' t know it this works, since I did not try it out (it is only an idea).
Nov 18, 2009 11:13 PM|DayAndNightCoder|LINK
Hi Chris and Daniel,
It is important to know what it is you want to do with the exception. Do you want to stop execution of the page load? Or do you want to just log the error and move on?
To stop execution of the page load, I have often used the Page_Error event. When an error causes the Page_Error event to fire, it usually means that the page cannot fully load and therefore cannot recover from the error. In these cases, I usually re-direct
them (using Respose.Redirect) to an error page that I have created. The error page tells the user that an error has occured and if appropriate, what the error was. By re-directing at this point, the original request is terminated and a new request is made.
Hence, the error never reaches the Global.asax error event.
If you want to catch the error and stop it from bubbling, then you should using try catch blocks to handle/log the error. These kinds of errors should be caught during database calls. Of course, if the error is caught and logged it also means that the Interface
layer did not get the information that was expected. If that happens then the Page_Error event would fire. If you want to load the page regardless of errors and missing information then you need to check for null on each setting of labels, text boxes etc.
In practice, it is better to alert the user that an error has occured and then for you to search for the actual cause.
So, the solution is to program your middle tier to log the errors that occured and then to stop the execution of a page request when the error has occured during loading of the page. The re-direction to a page that lets the user know something went wrong
and the logging of the error helps the support team to identify what went wrong.
One more thing. It is best to add an AppSetting that allows you to turn off the custom error handling. You may want to let the error bubble to the global.asax in various environments (such as QA or UAT) to help test users give relevant feedback. Then you
can do the following in your Page_Error function:
If ConfigurationManager.AppSettings("EnableCustomErrors").ToLower = "true" Then
'Redirect to Error Page