C#定义异常处理类

 学习来源:《CLR via C by Jeffrey Richter 》第四版,第20章  异常和状态管理

        [Serializable]
        public sealed class Exception : Exception, ISerializable where TexceptionArgs : ExceptionArgs {
            private const string c_args = "Args";
            private readonly TexceptionArgs m_args;

            public TexceptionArgs Args { get { return m_args; } }
            public Exception(string message = null, Exception innerException = null)
                : this(null, message, innerException)
            {

            }
            public Exception(TexceptionArgs args, string message = null, Exception innerException = null) : base(message, innerException) {
                m_args = args;
            }
            //这个构造器用于反序列化,由于类是密封的,所以构造器是私有的
            //如果这个类不是密封类,这个构造器需要受保护。
            [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
            private Exception(SerializationInfo info, StreamingContext context) : base(info, context) {
                m_args = (TexceptionArgs)info.GetValue(c_args, typeof(TexceptionArgs));
            }

            //这个方法用于序列化,由于ISerializable接口,所以它是公共的。
            [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
            public override void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                info.AddValue(c_args, m_args);
                base.GetObjectData(info, context);
            }

            public override string Message {
                get
                {
                    string basemesg = base.Message;
                    return (m_args == null) ? basemesg : basemesg + "[" + m_args.Message + "]";
                }
            }

            public override bool Equals(object obj)
            {
                Exception other = obj as Exception;
                if (other == null) {
                    return false;
                }
                return object.Equals(m_args, other.m_args) && base.Equals(obj);
            }
            public override int GetHashCode()
            {
                return base.GetHashCode();  
            }

        }
        [Serializable]
        public abstract class ExceptionArgs {
            public virtual string Message { get { return string.Empty; } }
        }

        //只继承不实现
        [Serializable]
        public sealed class DiskFullExcetionArgs2 : ExceptionArgs{ }
        //继承又实现
        [Serializable]
        public sealed class DiskFullExcetionArgs : ExceptionArgs {
            private readonly string m_diskpath;//在构造时设置的私有字段
            public DiskFullExcetionArgs(string diskpath) { m_diskpath = diskpath; }
            //返回字段的只读属性
            public string DiskPath { get { return m_diskpath; } }

            //重写message属性,来包含我们的字段(如果设置了的话)
            public override string Message {
                get {
                    return (m_diskpath == null) ? base.Message : "diskpath=" + base.Message;
                }
        }
        }
        //测试类
        public class MyTestEx {
            //使用Exception
            public void Testex (){
                try
                {
                    throw new Exception(new DiskFullExcetionArgs(@"C:\"), "this is disk error");
                }
                catch (Exception e)
                {

                    Console.WriteLine(e.Message);
                }
            }
        }

你可能感兴趣的:(c#,开发语言)