C# Global Exception Handler.

Posted on 2006-05-17
Last Modified: 2012-08-14
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#.
Question by:michellechan57
    LVL 48

    Accepted Solution

    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.
    LVL 13

    Expert Comment

    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.

    LVL 48

    Expert Comment

    devsolns, I am talking about following design rule:

    I will not continue this discussion because I prefer to talk with questioner and not with other participants.
    LVL 35

    Expert Comment

    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

       // 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

    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.

    Author Comment

    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.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now