C# 自定义Exception

有时需要区分不同的自定义错误,也可能需要特定的错误参数,此时需要自定义一些Exception类,而方法也很简单,简单的说需要继承Exception及其构造函数。
有时为了只调用特定参数,可以把其他构造函数设置为Private,即可隐藏。
public class CheckRuleException : Exception
    {
        public readonly ECheckRuleExceptionType CheckRuleExceptionType;
        public object Obj;
 
        public CheckRuleException(ECheckRuleExceptionType checkRuleExceptionType, object obj=null) : base()
        {
            this.CheckRuleExceptionType = checkRuleExceptionType;
            this.Obj = obj;
        }
        private CheckRuleException(string message) : base(message) { }
        private CheckRuleException(SerializationInfo info, StreamingContext context) : base(info, context) { }
        private CheckRuleException(string message, Exception innerException) : base(message, innerException) { }
    }

关于应用:
可以定义基类的方式,增加系统统一编码等,如:

 

posted @ 2017-08-25 12:12  sunlyk  阅读(2527)  评论(0编辑  收藏  举报