为什么TargetInvocationException由IDE视为未捕获的? [英] Why is TargetInvocationException treated as uncaught by the IDE?

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

问题描述

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

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;
                    }

最终,code正常工作,但是当我在调试器下运行:
当属性抛出一个异常,则IDE下降到调试器仿佛例外是未捕获的。如果我只是打运行,程序流经和异常出来,与在InnerException属性真正异常的TargetInvocationException。

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?

推荐答案

嗯,这似乎是在Visual Studio中的错误。感谢您让我们知道,我们正在寻找它。

Hm, it appears to be a bug in Visual Studio. Thanks for letting us know, we're looking at it.

编辑:我已经跟进,这似乎设计是。什么情况是,你可能有工具 - >选项 - >调试 - >常规 - >启用仅我的code

I've followed up and this seems to be "by design". What happens is that you likely have Tools -> Options -> Debugging -> General -> Enable Just My Code.

由于帮助文件在这里指出:
<一href=\"http://msdn.microsoft.com/en-us/library/038tzxdw.aspx\">http://msdn.microsoft.com/en-us/library/038tzxdw.aspx
调试 - >例外对话框中显示的附加列(休息的时候一个例外是用户未处理)当启用仅我的code是的。本质上,这意味着每当例外是离开code的边界(在此情况下,它属于通过向下到.NET框架反射code)中,VS打破,因为它认为该异常已离开用户code。它不知道它会在堆栈稍后返回到用户code。

As the help file here states: http://msdn.microsoft.com/en-us/library/038tzxdw.aspx 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), VS 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.

因此​​,有两种解决方法:禁止仅我的code在工具 - >选项 - >调试 - >常规或从调试的用户未处理的.NET Framework异常删除的复选框 - >异常对话框

So there are two workarounds: Disable Just My Code in Tools -> Options -> Debugging -> General or Remove the check box from the User-unhandled .NET Framework exceptions in the Debug -> Exceptions dialog.

希望这有助于!

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

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