我有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)
基本上我抛出一个异常从另一个例外。
这种方法不好吗?
+1,ApplicationNotFoundException应该派生自CustomException – 2013-05-11 10:18:19
@ bash.d:'ApplicationNotFoundException'派生自'CustomException'的唯一缺点是我无法在'ApplicationNotFoundException'构造函数体内创建异常文本 – Catalin 2013-05-11 10:20:47
@RaraituL你不能?为什么不?? – 2013-05-11 10:22:42