Click here to Skip to main content
15,881,204 members
Articles / Programming Languages / C#

To Err is Human… to Handle, Divine

Rate me:
Please Sign up or sign in to vote.
3.54/5 (5 votes)
9 Apr 2007CPOL10 min read 36.5K   73   32  
How do you handle exceptions? Do you reuse your solution?
using System;
using System.Collections.Generic;
using System.Text;

namespace Jason.EnterpriseErrorHandler
{
    public class ErrorManager 
    {
        /*Begin Global Base Methodology*/
        internal const int MEMORY_STORE_DEFAULT_SIZE = 4096;
        static private ErrorManager Signleton = new ErrorManager();
        
        static public ErrorManager InstallApplicationErrorHandler()
        {
            System.AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Signleton.ErrorHandler);
            //Signleton.attachedTo.Add(System.AppDomain.CurrentDomain);//We can be installed on many app domains? 
            return Signleton;
        }
        static public ErrorManager UnInstallApplicationErrorHandler()
        {
            //List<AppDomain> contextDomains = Signleton.attachedTo;
            //foreach( AppDomain apd in contextDomains)
            //    apd.UnhandledException -= new UnhandledExceptionEventHandler(Signleton.ErrorHandler);

            System.AppDomain.CurrentDomain.UnhandledException -= new UnhandledExceptionEventHandler(Signleton.ErrorHandler);
            return Signleton;
        }
        static public ErrorManager GetApplicationErrorHandler()
        {
            return Signleton;
        }        

        /*Begin Object Base Methodology*/
        Jason.EnterpriseErrorHandler.ConfigurationHandler.ErrorSubSystem cfg;
        System.Collections.Generic.List<System.AppDomain> attachedTo;

        private ErrorManager()
        {/*Called when singleton is first initilized*/
            try
            {
                attachedTo = new List<AppDomain>();
                cfg = System.Configuration.ConfigurationManager.GetSection("errorSubSystem")
                        as Jason.EnterpriseErrorHandler.ConfigurationHandler.ErrorSubSystem;
                if (cfg != null) System.Diagnostics.Debug.WriteLine(cfg.GetType().Name);
            }
            catch (Exception Ex)
            {
                System.Diagnostics.Debug.WriteLine(Ex.ToString());
            }

            return;
        }
         ~ErrorManager()
        {/*Since the Signleton holds a ref, this will only be called when the appdomin is asked to deconstruct.*/
            ErrorManager.UnInstallApplicationErrorHandler(); 
        }
        public void SimulateErrorCondition(Exception Ex){
            UnhandledExceptionEventArgs arg = new UnhandledExceptionEventArgs(Ex, false);
            this.ErrorHandler(this, arg);
        }
        private void ErrorHandler(Object sender, UnhandledExceptionEventArgs arg)
        {
            try
            {
                Exception Err = (Exception)arg.ExceptionObject;
                Type Key = arg.ExceptionObject.GetType();
                //iErrorPolicy Policy = null;
                while (Key != null)
                {
                    /*Lookup any Policies in order of most specific type first*/
                    //Policy = cfg.LookupPolicy(Key);
                    //if (Policy != null) Policy.ApplyInternalHandlerTo(Err);
                    if (cfg != null) cfg.invokeRelevantPolicy(Key,Err);

                    Key = Key.BaseType;/*Get More Generic Error Type*/
                }
            }
            catch (Exception Ex)
            {//Swallow errors because we are in a exception handler
                System.Diagnostics.Debug.WriteLine(Ex.ToString());
                //TODO: Merge the Argument error, with the current Ex error such that there is internal cause.
            }
        }
    }/*End ErrorManager*/
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Berkeley Grad (B.A. Computer Science) With MBA

Comments and Discussions