当C#抛出异常时,它可能有一个内部异常.我想要做的是获取最内部的异常,或者换句话说,没有内部异常的叶子异常.我可以在while循环中执行此操作:
while (e.InnerException != null)
{
e = e.InnerException;
}
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有一些我可以使用的单行代替.
我有这种类型的例外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)