gpt4 book ai didi

c# - 如何访问 InnerException 的 InnerExceptions 属性?

转载 作者:行者123 更新时间:2023-11-30 17:40:35 25 4
gpt4 key购买 nike

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

InnerException: Nothing
InnerExceptions: Count=1

然后我可以展开 InnerExceptions 分支并查看我假设的 AggregateException,在本例中显示

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

然后我可以展开 (0) 组来查看诸如“Detail”之类的属性,它提供完整且详细的错误消息以及“ErrorCode”和许多其他属性。

当我尝试通过 ex.InnerException.InnerExceptions 通过代码引用“InnerExceptions”时,我不能,因为“InnerExceptions”不是“Exception”的成员。

它如何在 Visual Studio IDE 中可见但通过代码不可用?

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

明确一点:遍历 InnerException 属性不会返回在上述“详细信息”中发现的相同错误。

InnerExceptions from Watch window

最佳答案

Exception 类没有名为 InnerExceptions 的成员,但它是 AggregateException 的基类其中有。 Visual Studio 的调试器将找出每个对象的类型,因此能够显示它们拥有的每个属性。

然而,Exception 类的 InnerException 成员不是 AggregateException 类型,只是一个通用的 Exception。也就是说,Visual Studio 无法确定您的 InnerException 是否实际上是类型的 AggregateException。要解决这个问题,您需要进行转换。

我不太熟悉 vb.net 语法,在 C# 领域它会像这样:

((AggregateException)ex.InnerException).InnerExceptions

或者你可以像这样安全地尝试转换:

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

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

关于c# - 如何访问 InnerException 的 InnerExceptions 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34203058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com