为什么 IDE 将 TargetInitationException 视为未捕获?

发布于 2024-08-29 22:56:58 字数 465 浏览 4 评论 0原文

我有一些代码使用反射从对象中提取属性值。在某些情况下,属性可能会引发异常,因为它们具有空引用等。

object result;
try
{
    result = propertyInfo.GetValue(target, null);

}
catch (TargetInvocationException ex)
{
    result = ex.InnerException.Message;
}
catch (Exception ex)
{
    result = ex.Message;
}

最终,代码可以正常工作,但是当我在调试器下运行时:

当属性引发异常时,IDE 会进入调试器,就好像异常是未被捕获。如果我只是点击运行,程序就会流过,并且异常会以 TargetInitationException 的形式出现,真正的异常位于 InnerException 属性中。

我怎样才能阻止这种情况发生?

I have some code that is using reflection to pull property values from an object. In some cases the properties may throw exceptions, because they have null references, etc.

object result;
try
{
    result = propertyInfo.GetValue(target, null);

}
catch (TargetInvocationException ex)
{
    result = ex.InnerException.Message;
}
catch (Exception ex)
{
    result = ex.Message;
}

Ultimately the code works correctly, however when I am running under the debugger:

When the property throws an exception, the IDE drops into the debugger as if the exception was uncaught. If I just hit run, the program flows through and the exception comes out as a TargetInvocationException with the real exception in the InnerException property.

How can I stop this from happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹倦 2024-09-05 22:56:58

这似乎是“设计使然”。发生的情况是,您可能有菜单工具选项调试常规仅启用我的代码已启用。

正如如何:中断用户未处理的异常< /em> 状态:

当“仅启用我的代码”打开时,调试异常对话框会显示一个附加列(用户未处理异常时中断)。

本质上,这意味着每当异常离开代码边界(在这种情况下,它会向下传递到 .NET 框架反射代码),Visual Studio 就会中断,因为它认为异常已离开用户代码。它不知道它稍后会返回到堆栈中的用户代码中。

因此有两种解决方法:在菜单 工具选项调试常规中禁用仅我的代码 从菜单调试异常中删除用户未处理的.NET Framework异常的复选框> 对话框。

This seems to be "by design". What happens is that you likely have menu ToolsOptionsDebuggingGeneralEnable Just My Code enabled.

As How to: Break on User-Unhandled Exceptions states:

The DebugExceptions dialog shows an additional column (Break when an exception is User-unhandled) when "Enable Just My Code" is on.

Essentially this means that whenever the exception is leaving the boundary of your code (and in this case, it falls through down to the .NET framework reflection code), Visual Studio breaks because it thinks that the exception has left the user code. It doesn't know that it will return into the user code later in the stack.

So there are two workarounds: Disable Just My Code in menu ToolsOptionsDebuggingGeneral or Remove the check box from the User-unhandled .NET Framework exceptions in menu DebugExceptions dialog.

魂ガ小子 2024-09-05 22:56:58

编辑:我自己刚刚尝试过,看起来反射的处理方式略有不同。就调试器而言,您可能希望将反射调用视为启动一个新的“处理”级别:在异常被翻译并作为 TargetInitationException 重新抛出之前,没有任何东西可以捕获该异常,因此它我不知道是否有任何方法可以抑制这种情况 - 但这种情况经常发生吗?如果您经常执行大量导致异常的操作,您可能需要重新考虑您的设计。


原始答案

转到“调试/异常...”并查看设置是什么。如果 TargetInitationException(或层次结构中更高的任何内容)选中了“Thrown”复选框,您将看到此行为。

EDIT: I've just tried this myself, and it looks like reflection is treated slightly differently. You might want to think of a reflection call as starting a new level of "handled" as far as the debugger is concerned: nothing is catching that exception before it gets translated and rethrown as a TargetInvocationException, so it breaks in. I don't know if there's any way of inhibiting that - but does it happen very often? If you're regularly performing lots of operations which result in exceptions, you might want to reconsider your design.


Original answer

Go to Debug / Exceptions... and see what the settings are. You'll see this behaviour if TargetInvocationException (or anything higher in the hierarchy) has the "Thrown" tickbox checked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文