Best way to check for inner exception?

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

    With C# 6.0 you can use:

    string message = exception.InnerException?.Message ?? "";

    This line of code is similar to:

    string message = exception.InnerException == null ? "" : exception.InnerException.Message.

    https://msdn.microsoft.com/en-us/library/ty67wk28.aspx

    http://blogs.msdn.com/b/jerrynixon/archive/2014/02/26/at-last-c-is-getting-sometimes-called-the-safe-navigation-operator.aspx

提交回复
热议问题