[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1984
  • Last Modified:

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#.
0
michellechan57
Asked:
michellechan57
1 Solution
 
AlexFMCommented:
See Application.ThreadException Event.
Warning: gloabl exception handling is not good idea, this breaks .NET programming guidelines. It is not recommended even to catch Exception in any case, only specific exception types which are expected. Global exception handler hides programmer bugs, and resulting Exception dialog is not much better than standard exception dialog.
I belewive that global exception handling can be used if it writes all relevant information (like Exception.StackTrace Property) to some log, which allows to keep information for fixing bugs.
0
 
devsolnsCommented:
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.

0
 
AlexFMCommented:
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.
0
 
mrichmonCommented:
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.
0
 
michellechan57Author Commented:
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now