我有一个CustomException
类,这是类顶部的包装器Exception
。这是我处理异常时使用的主要类。
public class CustomException : Exception
{
public string ErrorMessage { get; private set; }
public HttpStatusCode HttpStatusCode { get; private set; }
public CustomException(string errorMessage)
: this(errorMessage, HttpStatusCode.InternalServerError)
{ }
public CustomException(string message, HttpStatusCode httpStatusCode)
{
ErrorMessage = message;
HttpStatusCode = httpStatusCode;
}
}
当我想抛出异常时,我使用throw CustomException()
方法。
但是,我也想在这个 CustomException() 之上创建一些包装器,例如:
public class ApplicationNotFoundException : Exception
{
public ApplicationNotFoundException(Application application)
{
string message = string.Format(@"Application ""{0}"" was not found", application.ApplicationName);
throw new CustomException(message, HttpStatusCode.NotFound);
}
}
我抛出异常行:throw new ApplicationNotFoundException(application)
基本上我是从另一个异常中抛出一个异常。
这种方法不好吗?