Best way to check for inner exception?

后端 未结 14 2323
说谎
说谎 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:25

    Its an old question but for future readers:

    In addition to the answers already posted I think the correct way to do this (when you can have more than one InnerException) is Exception.GetBaseException Method

    If you want the exception instance you should do this:

    repEvent.InnerException = ex.GetBaseException();
    

    If you are only looking for the message this way:

    repEvent.InnerException = ex.GetBaseException().Message;
    

提交回复
热议问题