我想拋出一個異常,但自定義消息並堅持棧跟蹤。我已經通過各種線程。拋出異常,但堅持堆棧跟蹤
catch (Exception ex)
{
throw; // Message is read only but stacktrace persist
throw ex; // Message is readonly and strack trace also blows .. worst!
throw new CustomException("My exception");// Message can be overridden but stacktrace lost
throw new CustomException("My message",ex);// same as above. However if this constructor in exception class calls same constructor of base class then .. see below
}
當最後appraoch使用(與自定義異常構造調用基類的構造函數),輸出對死亡的屏幕是一樣的東西:
**The remote server returned an error: (401) Unauthorized.**
[WebException: The remote server returned an error: (401) Unauthorized.]
original stack trace
[NewException: newMessage]
New Stack Trace
的好處是一切都沒有在屏幕上。但是,最重要的是,我希望我的例外顯示「新消息」,而不是原始消息。
因此,調和我的問題:如何顯示在死亡的屏幕上的原始堆棧跟蹤,但與自定義錯誤消息?
這是一個Web服務返回的異常? – Oded
在我的例子中,它確實是一個web服務調用。然而,它是無關緊要的,因爲異常可能是像DivisionByZero或sqlException之類的東西。想法是讓用戶知道原始堆棧跟蹤,但是開發人員還可以自定義默認異常消息以獲得更多幫助。 – helloworld
不,它並不重要。 Webservice框架對異常有自己的想法。 –