Click here to Skip to main content
15,892,575 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.6K   73   32  
How do you handle exceptions? Do you reuse your solution?
using System;
using System.Collections.Generic;
using System.Text;

namespace Jason.EnterpriseErrorHandler
{
    class ErrorPolicy:iErrorPolicy
    {
        Type[] tp = (Type[])Array.CreateInstance(typeof(Type), 3);
        public ErrorPolicy(Type TriggerException, Type Fmter, Type Handler)
        {
            tp[0] = TriggerException;
            tp[1] = Fmter;
            tp[2] = Handler;
        }
        public ErrorPolicy() { return; }
        
        public Type TriggerError
        {
            get { return tp[0]; }
            set { tp[0] = value; }
        }

        public Type Formatter
        {
            get { return tp[1]; }
            set { tp[1] = value; }
        }

        public Type Handler
        {
            get { return tp[2]; }
            set { tp[2] = value; }
        }

        #region iErrorPolicy Members

        Type iErrorPolicy.GetTriggerExceptionType()
        {
            return tp[0];
        }

        void iErrorPolicy.ApplyInternalHandlerTo(Exception Error)
        {
            System.Text.StringBuilder sb = new StringBuilder();

            if (tp[1] == null) throw new System.InvalidOperationException("Invalid Error Policy Settings");
            if (tp[2] == null) throw new System.InvalidOperationException("Invalid Error Policy Settings");

            if (typeof(Jason.EnterpriseErrorHandler.iErrorFormatter).IsAssignableFrom(tp[1]))
            {
                /*Formatter*/
                Jason.EnterpriseErrorHandler.iErrorFormatter Fmter = (Jason.EnterpriseErrorHandler.iErrorFormatter)
                                                        tp[1].GetConstructor(System.Type.EmptyTypes).Invoke(new Object[0]);
                ErrorFormatterArgs FmtArgs= new ErrorFormatterArgs();
                FmtArgs.Ex = Error;
                FmtArgs.OutStream = new System.IO.StringWriter(sb);
                Fmter.FormatException(FmtArgs);
            }
            if (typeof(Jason.EnterpriseErrorHandler.iErrorHandler).IsAssignableFrom(tp[2]))
            {
                /*Handler*/
                Type[] constructorTypes = (Type[])Array.CreateInstance(typeof(Type), 1);
                object[] constructorArgs = (object[]) Array.CreateInstance(typeof(Object), 1);                

                constructorTypes[0] = typeof(ErrorWriterConstructorArgs);
                constructorArgs[0]= new ErrorWriterConstructorArgs();
                
                Jason.EnterpriseErrorHandler.iErrorHandler handler = (Jason.EnterpriseErrorHandler.iErrorHandler)
                                                        tp[2].GetConstructor(constructorTypes).Invoke(constructorArgs);
                ErrorHandlerArgs HndArgs = new ErrorHandlerArgs();
                HndArgs.Ex = Error;
                HndArgs.FormattedStream = new System.IO.StringReader(sb.ToString());
                handler.doErrorProcessing(HndArgs);
            }
            return;
        }

        

        #endregion
    }

    

}

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