Link to home
Start Free TrialLog in
Avatar of michellechan57
michellechan57

asked on

C# Global Exception Handler.

I want to migrate Delphi code to C#. Need advise on how to accomplish global exception handling in C#, refer to article titled: "Make a global exceptionhandle". Need to achieve the same task in C#.
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of devsolns
devsolns

Those are assumptions your making without knowing his implementation.

"It is not recommended even to catch Exception in any case, only specific exception types which are expected."

This blanket statement would be considered dead wrong in some instances and correct in others.  .NET doesn't use a consistent security model.  Some custom exceptions inherit type Exception, some from type ApplicationException.  It can get confusing.  Also if you needed to catch non CLS compliant exceptions then even catch{} with no type would be used so it really depends.

The important thing to keep in mind is dont just let things propagate up and leave things in an bad state.

try {
     //change something here.
}
finally {
     //change it back here.
}


Take a look at, AppDomain.CurrentDomain.UnhandledException event.

devsolns, I am talking about following design rule:
http://www.gotdotnet.com/team/fxcop/docs/rules.aspx?version=1.35&url=/Design/DoNotCatchGeneralExceptionTypes.html

I will not continue this discussion because I prefer to talk with questioner and not with other participants.
Global Exception handling is a great idea - as long as it is done properly.

No you do not want to just catch it to catch everything.  But you do want to know about it.

For example, the best case for global exception handling is on a website.  You NEVER want to allow even a System.Exception to be shown to the user.  Instead you want to log it, or notify the appropriate parties, and then direct the user to a friendly error page so that the error in your code is not exposed.  This is a good idea too for windows apps, but would be handled slightly differently.

Here is how you would do that for Web applications.

In the global.asax file you need the following:

protected void Application_Error(object sender, EventArgs e)
{
    // This function runs whenever there is an uncaught exception in the main protion of the code.
    // So here you put whatever code you need to log the exception
   // The last exception is available at Server.GetLastError().InnerException
    LogException(Server.GetLastError().InnerException);

   // Now that you have logged it or whatever, you can direct the user to a friendly page so they don't see the ugly exception screen
   Response.Redirect("Firendlyerrorpage.aspx");
}

So at this point you are not putting in an actual catch block for the System.Exception type - which is what the design rule AlexFM is referring to talks about, but you ARE doing global exception handling in a very valid method.
Avatar of michellechan57

ASKER

Hi guys, thanks for the fast response.
The original software design goal (in Delphi) to have the global exception handler was to catch all exceptions that are not caught, and when it happened, the software is allowed to continue to "function" and at the same time logged the error messages in a file for developer to review and enhance the software in the future. It is used for good intention and we would like to continue to practice this even if we migrate to any languages.