找到最内层异常的正确方法? [英] Proper way to find the innermost exception?

查看:103
本文介绍了找到最内层异常的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些类,当它们抛出时,会有一个比较深的InnerException树。我想记录并采取行动的最内层的例外,这是有问题的真正原因。



我正在使用类似于

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

这是处理异常树的正确方法吗?

解决方案

我想您可以使用以下代码获得最内部的异常:

  public static Exception getInnmostException(Exception e){
return e.GetBaseException();
}


I'm working with some classes which, when throwing, have a relatively deep InnerException tree. I'd like to log and act upon the innermost exception which is the one having the real reason for the problem.

I'm currently using something similar to

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

Is this the proper way to handle Exception trees?

解决方案

I think you can get the innermost exception using the following code:

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

这篇关于找到最内层异常的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆