检查内部异常的最好方法?异常、方法

2023-09-02 20:48:44 作者:截惊

我知道,有时的InnerException为null

I know sometimes innerException is null

所以下面可能失败:

 repEvent.InnerException = ex.InnerException.Message;

有没有一种快速的三元方法来检查的InnerException为空或不是?

Is there a quick ternary way to check if innerException is null or not?

推荐答案

这是你正在寻找什么?

String innerMessage = (ex.InnerException != null) 
                      ? ex.InnerException.Message
                      : "";