如何访问InnerException的InnerExceptions属性? [英] How do I access the InnerExceptions property of an InnerException?

查看:129
本文介绍了如何访问InnerException的InnerExceptions属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,使用典型的try..catch引发并捕获了异常.异常中的消息是执行查询时发生错误.请检查堆栈跟踪以获取更多详细信息.还有一个InnerException与消息 ValidationException被抛出..没有其他InnerException.但是,当我通过Visual Studio 2015查看异常时,可以展开该异常,转到InnerException并将其展开,然后看到:

In my code I have an exception thrown and caught using a typical try..catch. The message in the exception is An error occured while executing the query. Please check the stack trace for more detail. There is also an InnerException with the message ValidationException was thrown. There is no other InnerException. However, when I look at the exception through Visual Studio 2015, I can expand the Exception, go to the InnerException and expand that and I see:

InnerException: Nothing
InnerExceptions: Count=1

然后我可以展开InnerExceptions分支,看看我假设是一个AggregateException,在这种情况下,它将显示

I can then expand the InnerExceptions branch and see what I assume is an AggregateException which, in this case, shows

(0): {"Error Parsing query"}
Raw View: 

然后,我可以展开(0)组以查看诸如详细信息"之类的属性,该属性给出完整而详细的错误消息以及错误代码"和许多其他内容.

I can then expand the (0) group to see properties like "Detail" which gives a full and detailed error message as well as "ErrorCode" and many others.

当我尝试通过 ex.InnerException.InnerExceptions 通过代码引用"InnerExceptions"时,我不能这样做,因为"InnerExceptions"不是"Exception"的成员.

When I try to reference the "InnerExceptions" through code via ex.InnerException.InnerExceptions, I can't because 'InnerExceptions' is not a member of 'Exception'.

它在Visual Studio IDE中如何显示,但不能通过代码使用?

How is it visible in the Visual Studio IDE but not available through code?

我正在编写使用IppDotNetSdkForQuickBooksApiV3 NuGet包的代码.我提到这一点是因为我不确定这是否是从Intuit的API中添加的.我以前从未遇到过 InnerExceptions 组.

I am writing code that is using the IppDotNetSdkForQuickBooksApiV3 NuGet package. I mention this since I am not sure if this is somehow added on from Intuit's API or not. I've never encountered an InnerExceptions group before.

请注意:遍历InnerException属性不会返回上述详细信息"中发现的相同错误.

Just to be clear: Iterating through the InnerException property does not return the same error found in the "Detail" mentioned above.

推荐答案

Exception 类没有名为InnerExceptions的成员,但是它是

The Exception class doesn't have a member called InnerExceptions, however it is a base class of AggregateException which has. Visual Studio's debugger will figure out each objects' type and therefore is able to display every property they have.

但是,Exception类的InnerException成员不是AggregateException类型的,而只是普通的Exception.就是说,Visual Studio无法根据类型确定您的InnerException实际上是否将是AggregateException.要解决此问题,您需要进行投射.

However the Exception class's InnerException member is not of an AggregateException type, only a generic Exception. That said, Visual Studio has no way to figure out if your InnerException is going to actually be an AggregateException by type. To solve this, you need to cast.

我对vb.net语法不是很熟悉,在C#中会这样:

I'm not really familiar with vb.net syntax, in C# land it would like this:

((AggregateException)ex.InnerException).InnerExceptions

或者您可以尝试像这样安全地投射:

or you can try safely casting like so:

if (ex.InnerException.GetType() == typeof(AggregateException)) 
{
    var listOfInnerExceptions = ((AggregateException)ex.InnerException).InnerExceptions;
}

据我所知,VB.net中有一个 DirectCast(obj,type)方法可用于此目的,但我对此可能是错的.

As far as I know there is a DirectCast(obj, type) method in VB.net that could be used for this purpose, but I might be wrong about this.

这篇关于如何访问InnerException的InnerExceptions属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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