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