using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace CY.Infrastructure.PException
{
///
/// 异常模块异常,框架的基础异常类,所有的异常请从本类派生
///
[Serializable]
public class ExceptionBase : Exception
{
///
/// 构造函数
///
public ExceptionBase()
: this(0, null, null)
{
}
///
/// 构造函数
///
/// 异常消息
/// 内部异常
public ExceptionBase(string message, Exception innerException)
: this(0, message, innerException)
{
}
///
/// 构造函数
///
/// 异常消息
public ExceptionBase(string message)
: this(0, message)
{
}
///
/// 构造函数
///
/// 异常编号
/// 异常消息
public ExceptionBase(int errorNo, string message)
: this(errorNo, message, null)
{
}
///
/// 构造函数
///
/// 异常编号
/// 异常消息
/// 内部异常
public ExceptionBase(int errorNo, string message, Exception innerException)
: base(message, innerException)
{
this.ErrorNo = errorNo;
}
///
/// 序列化和反序列化时的构造函数
///
/// The System.Runtime.Serialization.SerializationInfo that holds the serialized object data about the exception being thrown.
/// The System.Runtime.Serialization.StreamingContext that contains contextual information about the source or destination.
protected ExceptionBase(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
///
/// 异常编号
///
public int ErrorNo { get; protected set; }
///
/// 查找原始的异常
///
/// 异常
/// 原始的异常
public static Exception FindSourceException(Exception e)
{
var e1 = e;
while (e1 != null)
{
e = e1;
e1 = e1.InnerException;
}
return e;
}
///
/// 从异常树种查找指定类型的异常
///
/// 异常
/// 期待的异常类型
/// 所要求的异常,如果找不到,返回null
public static Exception FindSourceException(Exception e, Type expectedExceptionType)
{
while (e != null)
{
if (e.GetType() == expectedExceptionType)
{
return e;
}
e = e.InnerException;
}
return null;
}
}
}