username@email.com
2025-05-21 a980cd04341d71216e0f59bd4b7327fe9fc50032
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
 
namespace CY.Infrastructure.PException
{
    /// <summary>
    /// 异常模块异常,框架的基础异常类,所有的异常请从本类派生
    /// </summary>
    [Serializable]
    public class ExceptionBase : Exception
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ExceptionBase()
            : this(0, null, null)
        {
        }
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="message">异常消息</param>
        /// <param name="innerException">内部异常</param>
        public ExceptionBase(string message, Exception innerException)
            : this(0, message, innerException)
        {
        }
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="message">异常消息</param>
        public ExceptionBase(string message)
            : this(0, message)
        {
        }
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="errorNo">异常编号</param>
        /// <param name="message">异常消息</param>
        public ExceptionBase(int errorNo, string message)
            : this(errorNo, message, null)
        {
        }
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="errorNo">异常编号</param>
        /// <param name="message">异常消息</param>
        /// <param name="innerException">内部异常</param>
        public ExceptionBase(int errorNo, string message, Exception innerException)
            : base(message, innerException)
        {
            this.ErrorNo = errorNo;
        }
 
        /// <summary>
        /// 序列化和反序列化时的构造函数
        /// </summary>
        /// <param name="info">The System.Runtime.Serialization.SerializationInfo that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The System.Runtime.Serialization.StreamingContext that contains contextual information about the source or destination.</param>
        protected ExceptionBase(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
 
        /// <summary>
        /// 异常编号
        /// </summary>
        public int ErrorNo { get; protected set; }
 
        /// <summary>
        /// 查找原始的异常
        /// </summary>
        /// <param name="e">异常</param>
        /// <returns>原始的异常</returns>
        public static Exception FindSourceException(Exception e)
        {
            var e1 = e;
            while (e1 != null)
            {
                e = e1;
                e1 = e1.InnerException;
            }
            return e;
        }
 
        /// <summary>
        /// 从异常树种查找指定类型的异常
        /// </summary>
        /// <param name="e">异常</param>
        /// <param name="expectedExceptionType">期待的异常类型</param>
        /// <returns>所要求的异常,如果找不到,返回null</returns>
        public static Exception FindSourceException(Exception e, Type expectedExceptionType)
        {
            while (e != null)
            {
                if (e.GetType() == expectedExceptionType)
                {
                    return e;
                }
                e = e.InnerException;
            }
            return null;
        }
    }
}