为什么TargetInvocationException被IDE忽略? [英] Why is TargetInvocationException treated as uncaught by the IDE?

查看:144
本文介绍了为什么TargetInvocationException被IDE忽略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 对象结果; 
try
{
result = propertyInfo.GetValue(target,null);

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

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

当该属性抛出异常时,IDE将丢弃到调试器中,就像未被捕获的异常一样。如果我只是运行,程序就会流过,异常就会出现在TargetInvocationException中,并且在InnerException属性中有真正的异常。



我如何阻止这种情况发生?

解决方案

这似乎是按设计。会发生什么是您可能有菜单工具选项调试常规启用我的



As 如何:打破用户未处理的异常 说明:



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



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



所以有两个解决方法:禁用只是我的代码在菜单工具选项调试常规 strong>从菜单调试例外对话框中的用户未处理的.NET Framework异常中删除复选框。


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?

解决方案

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.

这篇关于为什么TargetInvocationException被IDE忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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