检查内部异常的最佳方法是什么?

JL.*_*JL. 56 .net c# exception-handling exception

我知道有时innerException是null

因此以下可能会失败:

 repEvent.InnerException = ex.InnerException.Message; 
Run Code Online (Sandbox Code Playgroud)

有没有快速的三元方法来检查innerException是否为null?

jri*_*sta 80

到目前为止很棒的答案 在类似但不同的注释中,有时存在多个嵌套异常级别.如果您想获得最初抛出的根异常,无论多深,您可以尝试这样做:

public static class ExceptionExtensions
{
    public static Exception GetOriginalException(this Exception ex)
    {
        if (ex.InnerException == null) return ex;

        return ex.InnerException.GetOriginalException();
    }
}
Run Code Online (Sandbox Code Playgroud)

在使用中:

repEvent.InnerException = ex.GetOriginalException();
Run Code Online (Sandbox Code Playgroud)

  • 他说你应该实现自己的方法GetOriginalException.但是,看起来好像GetBaseException()做同样的事情. (3认同)
  • 我刚刚有一个示例,我尝试将 GetBaseException 用于 HttpClient 的 Task<String>,但它继续只返回更高级别的异常。当我实施此解决方案时,它返回了我正在寻找的“机器主动拒绝..”消息。杰瑞斯塔干得好! (2认同)

And*_*are 55

这是你想要的?

String innerMessage = (ex.InnerException != null) 
                      ? ex.InnerException.Message
                      : "";
Run Code Online (Sandbox Code Playgroud)

  • 另外,是不是只有我,或者如果参数被翻转并且 `!=` 更改为 `==`,它看起来会更清晰一些。 (2认同)

csh*_*net 43

这很有趣,我找不到Exception.GetBaseException()的任何问题?

repEvent.InnerException = ex.GetBaseException().Message;
Run Code Online (Sandbox Code Playgroud)


Nol*_*rin 16

最简单的解决方案是使用基本条件表达式:

repEvent.InnerException = ex.InnerException == null ? 
    null : ex.InnerException.Message;
Run Code Online (Sandbox Code Playgroud)


Yau*_*aur 12

为什么这些答案中的递归如此之多?

public static class ExceptionExtensions
{
    public static Exception GetOriginalException(this Exception ex)
    {
        while(ex.InnerException != null)ex = ex.InnerException;
        return ex;
    }
}
Run Code Online (Sandbox Code Playgroud)

似乎是一种更直接的方式来实现这一点.


Die*_*ego 11

这是一个老问题,但对于未来的读者:

除了已经发布的答案,我认为正确的方法(当你可以有多个InnerException时)是Exception.GetBaseException方法

如果你想要异常实例,你应该这样做:

repEvent.InnerException = ex.GetBaseException();
Run Code Online (Sandbox Code Playgroud)

如果您只是这样寻找消息:

repEvent.InnerException = ex.GetBaseException().Message;
Run Code Online (Sandbox Code Playgroud)


Tod*_*ams 8

使用C#6.0,您可以使用:

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

这行代码类似于:

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


Jan*_*nda 5

有时InnerException也有一个InnerException,所以你可以使用递归函数:

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


neu*_*t47 5

使用此代码,您可以确定您没有丢失任何内部异常消息

catch (Exception exception)
{
   Logger.Error(exception.Message);
   while (exception.InnerException != null)
   {
       exception = exception.InnerException;
       Logger.Error(exception);
   }
}
Run Code Online (Sandbox Code Playgroud)


Ash*_*win 5

使用C#6.0,您可以一行完成。

repEvent.InnerException = ex.InnerException?.Message; 
Run Code Online (Sandbox Code Playgroud)

有关C#6.0的其他功能,请单击此处