找到最内的异常而不使用而循环? [英] Find the inner-most exception without using a while loop?
本文介绍了找到最内的异常而不使用而循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#中抛出一个异常,它可以有一个内部异常。我想要做的就是最内的异常,或换句话说,将叶片异常,不具有内异常。我可以在while循环做到这一点:
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.
如格伦McElhoe这个答案说,这是唯一可靠的方法。
As said in this answer by Glenn McElhoe, it's the only reliable way.
这篇关于找到最内的异常而不使用而循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文