我不確定這是否可能。 (我認爲它應該)。如果不按照以下方式拋出新的產品,是否有可能抓住客戶的觀點?在.NET中沒有新投入的自定義異常
try
{
//Some logic which throws exception
}
catch (Exception)
{
throw new CustomException();
}
我想什麼有如下:
try
{
//Some logic which throws exception
}
catch (CustomException)
{
// Handles the exception
}
我已經嘗試了上面,但它不是直接抓我CustomException。我不得不做「throw new CustomException()
」這是不理想的。我究竟做錯了什麼?
我的自定義異常類看起來如下:
[Serializable]
public class CustomException : Exception
{
public CustomException()
: base() { }
public CustomException(string message)
: base(message) { }
public CustomException(string format, params object[] args)
: base(string.Format(format, args)) { }
public CustomException(string message, Exception innerException)
: base(message, innerException) { }
public CustomException(string format, Exception innerException, params object[] args)
: base(string.Format(format, args), innerException) { }
protected CustomException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
}
感謝,
問題是什麼?你的第二個代碼片段看起來很好。 – dlev 2013-05-13 08:13:25
對不起,只是編輯我的問題。 – daehaai 2013-05-13 08:14:54
代碼是否拋出CustomException?或者是拋出一個不同的異常,你想變成一個CustomException?如果是後者,那麼第二種方法就行不通了。 – dlev 2013-05-13 08:16:12