0

When using property reflection to SetValue, the property throws a TargetInvocationException. However, since the call to SetValue is an invocation, the exception is caught and not handled in the property. Is there way to handle the Target Exception in the property and have it ONLY thrown in the main program?

I want this throw to be as if I just made a method call, not an invocation.

Edit for clarification:

The problem I am having is that within the reflect class, I am getting a debug message that says "Exception was unhandled by user code". I have to 'continue' with the debug session and the inner exception is the 'real' exception. Is this just to be expected? I dont want to get warned (and I dont want to hide warnings), I want the code to fix the warning.

public class reflect
{
    private int _i;
    public int i
    {
        get { return _i; }
        set 
        { 
            try{throw new Exception("THROWN");}
            catch (Exception ex)
            { // Caught here ex.Message is "THROWN"
                throw ex; // Unhandled exception error DONT WANT THIS
            } 
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        reflect r = new reflect();
        try
        {
            r.GetType().GetProperty("i").SetValue(r, 3, null);
        }
        catch(Exception ex)
        { // Caught here, Message "Exception has been thrown by the target of an invocation"
            // InnerMessage "THROWN"
            // WANT THIS Exception, but I want the Message to be "THROWN"
        }
    }
}
Tizz
  • 820
  • 1
  • 15
  • 31
  • This isn't a warning in the normal sense of the word (i.e. something displayed at compile-time) - it's just the debugger breaking in (which wasn't *at all* clear in your first version). I believe this is a duplicate of another SO question, but it may be tricky to find. – Jon Skeet Jun 26 '14 at 21:40

2 Answers2

2

You need the InnerException:

catch(Exception ex)
{
    if (ex.InnerException != null)
    {
        Console.WriteLine(ex.InnerException.Message);
    }
}

This isn't specific to reflection - it's the general pattern for any exception which was caused by another. (TypeInitializationException for example.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Yes, the inner exception is the way to get the error. What I am trying to do is eliminate the "Unhandled exception" error within the reflect class. ^^^See my clarification above – Tizz Jun 26 '14 at 21:33
  • Thanks Jon. The original question link (marking this as a duplicate) was very helpful! – Tizz Jun 27 '14 at 17:18
0

Sorry, can't comment yet. Two things: 1) why are you first catching ex in your reflection class and then throwing it again? This shouldn't be the problem, though. 2) I think you are getting your exception. Check the "Exception has been thrown"'s inner exception.

doubleYou
  • 310
  • 1
  • 4
  • 14
  • 1) That was just an example to verify that I have a try catch, and that the catch is catching the error. 2) I want my Main code to handle the exception WITHOUT a "Unhandeled exception" error in the reflect class. ^^^See my clarification above – Tizz Jun 26 '14 at 21:35
  • But if you leave the code as you have it above, then it should work, shouldn't it? (I mean you can access the inner exception in Program an you shouldn't get the unhandled exception problem)? Or am I missing something. – doubleYou Jun 26 '14 at 21:44