相关疑难解决方法(0)

在不使用while循环的情况下找到最内部的异常?

当C#抛出异常时,它可能有一个内部异常.我想要做的是获取最内部的异常,或者换句话说,没有内部异常的叶子异常.我可以在while循环中执行此操作:

while (e.InnerException != null)
{
    e = e.InnerException;
}
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有一些我可以使用的单行代替.

.net c# exception-handling exception while-loop

76
推荐指数
5
解决办法
4万
查看次数

Exception.GetBaseException()以非null InnerException返回异常

我有这种类型的例外System.AggregateException:

Message = "One or more errors occurred."
Source = null
StackTrace = null
Run Code Online (Sandbox Code Playgroud)

InnerException是这样的System.Reflection.TargetInvocationException:

Message = "Exception has been thrown by the target of an invocation."
Source = "mscorlib"
StackTrace = 
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Incoming(IHubIncomingInvokerContext context)
Run Code Online (Sandbox Code Playgroud)

InnerException是我创建的这个例外,它来自ApplicationException:

Message = …
Run Code Online (Sandbox Code Playgroud)

exception c#-4.0

6
推荐指数
2
解决办法
4984
查看次数

标签 统计

exception ×2

.net ×1

c# ×1

c#-4.0 ×1

exception-handling ×1

while-loop ×1