DCSIMG
User Defined Exception Class - Gady Elkarif's Blog

Gady Elkarif's Blog

User Defined Exception Class

User Defined Exception Class

Let's say you want to define Serializable Exception-Derived type MyException, and you want it to inherits from class exception.

The following class sameple show how to derived new serializable class from class Exception:

    [Serializable] // Making your exception serializable

    public class MyException : Exception, ISerializable

    {

        private String m_errorId;

 

        public String ErrorId

        {

            get { return m_errorId; }

        }

 

        public override String Message

        {

            get

            {

                if (m_errorId == null)

                {

                    return base.Message;

                }

 

                StringBuilder msg = new StringBuilder(base.Message);

 

                msg.AppendFormat(" [Error Id = {0}]{1}", m_errorId, Environment.NewLine);

 

                return msg.ToString();

            }

        }

 

        public MyException()

            : base()

        {

        }

 

        public MyException(String message)

            : base(message)

        {

        }

 

        public MyException(String message, Exception innerException)

            : base(message, innerException)

        {

        }

 

        public MyException(String message, String errorId)

            : this(message)

        {

            m_errorId = errorId;

        }

 

        public MyException(String message, String errorId, Exception innerException)

            : this(message, innerException)

        {

            m_errorId = errorId;

        }

 

        // This protected constructor is used for deserialization.

        protected MyException(SerializationInfo info, StreamingContext context)

            : base(info, context)

        {

            if (info == null)

            {

                throw new System.ArgumentNullException("info");

            }

 

            m_errorId = info.GetString("ErrorId");

        }

 

        #region ISerializable Members

 

        // Describes set of security permissions applied to the code.

        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)

        {

            if (info == null)

            {

                throw new System.ArgumentNullException("info");

            }

 

            info.AddValue("ErrorId", m_errorId);

 

            base.GetObjectData(info, context);

        }

 

        #endregion

    }

Usage:

        static void Main(string[] args)

        {

            try

            {

                ThrowableMethod();

            }

            catch (MyException ex)

            {

                // Serialize

                BinaryFormatter fmt = new BinaryFormatter();

                FileStream fs = new FileStream(@"c:\MyExceprion.xml", FileMode.OpenOrCreate);

 

                fmt.Serialize(fs, ex);

 

                fs.Close();

 

                // Deserialize.

                fs = new FileStream(@"c:\MyExceprion.xml", FileMode.OpenOrCreate);

 

                MyException ex2 = (MyException)fmt.Deserialize(fs);

 

                fs.Close();

            }

        }

 

        private static void ThrowableMethod()

        {

            throw new MyException("Error in ThrowableMethod", "55");

        }

 

פורסם: Apr 29 2008, 05:16 PM by egady | with no comments
תגים:,
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 5 and 6 and type the answer here:


Enter the numbers above: