Best way to check for inner exception?

后端 未结 14 2315
说谎
说谎 2020-12-02 11:57

I know sometimes innerException is null

So the following might fail:

 repEvent.InnerException = ex.InnerException.Message; 

Is ther

14条回答
  •  爱一瞬间的悲伤
    2020-12-02 12:28

    Sometimes also InnerException has an InnerException, so you can use a recursive function for it:

    public string GetInnerException(Exception ex)
    {
         if (ex.InnerException != null)
         {
            return string.Format("{0} > {1} ", ex.InnerException.Message, GetInnerException(ex.InnerException));
         }
       return string.Empty;
    }
    

提交回复
热议问题