我有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