zoukankan      html  css  js  c++  java
  • 20、异常和状态管理

    namespace CLR_Via
    {
        class Class4
        {
            static void Main()
            {
                DiskFullException.TestException();
                Console.ReadKey();
            }
        }
        [Serializable]
        public sealed class DiskFullException : ExceptionArgs
        {
            private readonly string m_diskPath;
    
            public DiskFullException(string diskPath)
            {
                this.m_diskPath = diskPath;
            }
    
            public string DiskPath { get { return m_diskPath; } }
    
            public override string Message
            {
                get
                {
                    return m_diskPath == null ? base.Message : "DiskPath = " + m_diskPath;
                }
            }
    
            public static void TestException()
            {
                try
                {
                    throw new Exception<DiskFullException>(new DiskFullException(@"C:"), "this disk is full");
                }
                catch (Exception<DiskFullException> ex)
                {
                    Console.WriteLine(ex.Message);
                    throw;
                }
            }
        }
        public sealed class Exception<TExceptionArgs> : 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));
            }
    
            [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 baseMsg = base.Message;
                    return (m_args == null) ? baseMsg : string.Format("{0} {1} ", baseMsg, m_args.Message);
                }
            }
    
            public override bool Equals(object obj)
            {
                Exception<TExceptionArgs> other = obj as Exception<TExceptionArgs>;
                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; } }
        }
    }
    

      

  • 相关阅读:
    Mutex 的正确打开方式
    常用 CMD 命令
    【LeetCode】在排序数组中查找元素的第一个和最后一个位置【三次二分】
    【LeetCode】搜索旋转排序数组【两次二分】
    【手写代码】计算1-n中总共有多少二进制1
    【手写代码】快速计算数字x有多少个二进制1
    【手写代码】建立二叉搜索树
    【LeetCode】下一个排列【找规律】
    【LeetCode】删除排序数组中的重复项&&移除特定元素【双指针,原地算法】
    【LeetCode】删除链表的倒数第N个节点【双指针法】
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/9886502.html
Copyright © 2011-2022 走看看