为什么 IDE 将 TargetInitationException 视为未捕获?
我有一些代码使用反射从对象中提取属性值。在某些情况下,属性可能会引发异常,因为它们具有空引用等。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是“设计使然”。发生的情况是,您可能有菜单工具→选项→调试→常规→仅启用我的代码已启用。
正如如何:中断用户未处理的异常< /em> 状态:
当“仅启用我的代码”打开时,调试 → 异常对话框会显示一个附加列(用户未处理异常时中断)。
本质上,这意味着每当异常离开代码边界(在这种情况下,它会向下传递到 .NET 框架反射代码),Visual Studio 就会中断,因为它认为异常已离开用户代码。它不知道它稍后会返回到堆栈中的用户代码中。
因此有两种解决方法:在菜单 工具 → 选项 → 调试 → 常规中禁用仅我的代码 或从菜单调试→异常中删除用户未处理的.NET Framework异常的复选框> 对话框。
This seems to be "by design". What happens is that you likely have menu Tools → Options → Debugging → General → Enable Just My Code enabled.
As How to: Break on User-Unhandled Exceptions states:
The Debug → Exceptions 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 Tools → Options → Debugging → General or Remove the check box from the User-unhandled .NET Framework exceptions in menu Debug → Exceptions dialog.
编辑:我自己刚刚尝试过,看起来反射的处理方式略有不同。就调试器而言,您可能希望将反射调用视为启动一个新的“处理”级别:在异常被翻译并作为
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.