Why is TargetInvocationException treated as uncaught by the IDE?

Posted by Jason Coyne on Stack Overflow See other posts from Stack Overflow or by Jason Coyne
Published on 2010-04-17T15:31:57Z Indexed on 2010/04/17 15:43 UTC
Read the original article Hit count: 220

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.

                    try
                    {
                        child.Target = propertyInfo.GetValue(target, null);

                    }
                    catch (TargetInvocationException ex)
                    {
                        child.Target = ex.InnerException.Message;
                    }
                    catch (Exception ex)
                    {
                        child.Target = 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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection