簡體   English   中英

使用 InnerException 拋出異常 c#

[英]throw Exception with InnerException c#

我正在處理將Exception.InnerException存儲到 db 的代碼:

try
{        
    // what should I throw here?
}
catch (Exception ex)
{
    errorService.Save(ex.InnerException);
}

我應該拋出什么異常(異常類型)來檢查我的代碼?

throw new ArgumentException(); // what should I throw here?

這個例子不是很清楚,一個 ArgumentException 很少會有內部異常。 但是當你真的想要:

 new ArgumentException("parameter-name", previouseException);

或者可能

new Exception("I'm just a wrapper", new ArgumentException("parameter-name"));

在處理部分,如果沒有InnerException,或者InnerException又出現了InnerException怎么辦?

你可能想要的:

catch (Exception ex)
{
    while(ex.InnerException != null)  ex = ex.InnerException;
    errorService.Save(ex);
    // throw; here unless you're very sure about handling everything 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM