找到最大的异常,而不使用while循环? [英] Find the inner-most exception without using a while loop?

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

问题描述

当C#抛出异常时,它可能有一个内部异常。我想要做的是获得最内在的异常,也就是说,没有内部异常的叶子异常。我可以在一段时间循环中执行此操作:

When C# throws an exception, it can have an inner exception. What I want to do is get the inner-most exception, or in other words, the leaf exception that doesn't have an inner exception. I can do this in a while loop:

while (e.InnerException != null)
{
    e = e.InnerException;
}

但是我想知道是否有一个单行我可以使用

But I was wondering if there was some one-liner I could use to do this instead.

推荐答案

Oneliner:)

Oneliner :)

while (e.InnerException != null) e = e.InnerException;

显然,你不能使它更简单。

Obviously, you can't make it any simpler.

正如Glenn McElhoe的这个答案所述,这是唯一可靠的方式。

As said in this answer by Glenn McElhoe, it's the only reliable way.

这篇关于找到最大的异常,而不使用while循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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