Question

I want to throw all the form errors to the event log. To achieve this, I added code to Application_Error handler to send the error text to event log. I would like to know how to throw the exception to be caught in the Application_Handler without disrupting the normal flow of the page lifecycle.

I tried simply throwing ApplicationException:

throw new ApplicationException(String.Format("Http Request failed. {0}", userStatusErrorMessage));

It worked in the sense that the error details was added to the event log, but the page that threw the exception was not displaying anything after throwing the error. It was blank.

Any ideas? Much appreciated. Thank you and happy weekend!

Was it helpful?

Solution

Handling exceptions in global.asax would mean that you cannot go on " without disrupting the normal flow of the page lifecycle". That approach makes sense when you want to log the exception and then redirect to the configured custom error-page.

If you only want to log the exception and then act as if nothing had happened, you must catch the exception and call your logging method there. But i would suggest against it because an exception should be visible for the user.

OTHER TIPS

If it's an exception that the page can continue on after handling, then it's best kept away from global. If you want to log it, log it where you caught it (if you aren't using a logging framework from a third party, you can still use a static method so you need only add one line to your handler).

Leave global.asax for those you don't catch, and send an error page (500 for "I messed up, and I'll try to fix it, something in the 4xx range for "you messed up, so there's nothing I can do with this" - if you don't know its their fault, it's your fault; if it's really their fault and you don't know that, then that's your fault for not knowing).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top