2010-01-18 39 views
7

我有一些类其中,扔的时候,有一个比较深的InnerException树工作。我想登录并采取行动处理最具有问题真实原因的例外情况。找到最内层异常的正确方法?

我目前使用类似于

public static Exception getInnermostException(Exception e) { 
    while (e.InnerException != null) { 
     e = e.InnerException; 
    } 
    return e; 
} 

这是处理异常树木的正确方法是什么

+0

可能重复的[查找最内层的异常,而不使用while循环?](http://stackoverflow.com/questions/3876456/find-the-inner-most-exception-without-using- A-while循环) – DavidRR 2015-12-07 18:42:10

+0

如果有的话,它的另外一个重复的,这其中的老年人和有正确答案为接受。 – 2015-12-08 11:26:38

+0

问题年龄并不总是指定重复的控制标准。例如,考虑另一个问题的数量是这个数字的十倍以上。另外,被接受的答案只反映了提问者的意见。最后,请注意,另一个问题的[最高投票答案](http://stackoverflow.com/a/5792456/1497596)也提供了'GetBaseException()',但是在某些情况下表明了它的局限性。 – DavidRR 2015-12-08 13:15:15

回答

10

我想你可以用下面的代码获得最里面的异常:

public static Exception getInnermostException(Exception e) { 
    return e.GetBaseException(); 
} 
+1

谢谢,我需要先阅读更多API文档,然后再提问:) – 2010-01-18 10:39:11

0

一句话,是的。我想不出任何明显更好或不同的方法。除非你想把它作为一个扩展方法来代替,但它实际上是六个,其他的是六个。

3

你可以使用GetBaseException方法。 非常快例如:

try 
{ 
    try 
    { 
     throw new ArgumentException("Innermost exception"); 
    } 
    catch (Exception ex) 
    { 
     throw new Exception("Wrapper 1",ex); 
    } 
} 
catch (Exception ex) 
{ 
    // Writes out the ArgumentException details 
    Console.WriteLine(ex.GetBaseException().ToString()); 
} 
+0

+1,我更喜欢其他答案,所以我才接受它。 – 2010-01-18 10:39:53

+0

够公平的,不能与此争论:)只是想我会举一些示例代码/测试工具来展示它的行动。 – AdaTheDev 2010-01-18 10:41:42

0

有迹象表明,可以有多个的根本原因(例如AggregateExceptionReflectionTypeLoadException)例外。

我创建了自己class导航树,然后不同的访问者吸引到收集全部或仅仅根源。样本输出here。下面的相关代码片段。

public void Accept(ExceptionVisitor visitor) 
{ 
    Read(this.exception, visitor); 
} 

private static void Read(Exception ex, ExceptionVisitor visitor) 
{ 
    bool isRoot = ex.InnerException == null; 
    if (isRoot) 
    { 
     visitor.VisitRootCause(ex); 
    } 

    visitor.Visit(ex); 
    visitor.Depth++; 

    bool isAggregateException = TestComplexExceptionType<AggregateException>(ex, visitor, aggregateException => aggregateException.InnerExceptions); 
    TestComplexExceptionType<ReflectionTypeLoadException>(ex, visitor, reflectionTypeLoadException => reflectionTypeLoadException.LoaderExceptions); 

    // aggregate exceptions populate the first element from InnerExceptions, so no need to revisit 
    if (!isRoot && !isAggregateException) 
    { 
     visitor.VisitInnerException(ex.InnerException); 
     Read(ex.InnerException, visitor); 
    } 

    // set the depth back to current context 
    visitor.Depth--; 
} 

private static bool TestComplexExceptionType<T>(Exception ex, ExceptionVisitor visitor, Func<T, IEnumerable<Exception>> siblingEnumerator) where T : Exception 
{ 
    var complexException = ex as T; 
    if (complexException == null) 
    { 
     return false; 
    } 

    visitor.VisitComplexException(ex); 

    foreach (Exception sibling in siblingEnumerator.Invoke(complexException)) 
    { 
     visitor.VisitSiblingInnerException(sibling); 
     Read(sibling, visitor); 
    } 

    return true; 
} 
相关问题