1

I am using reflection to set properties on an object. If any of the setters throw an exception, the exception is not caught by the code that makes the SetValue call. Visual Studio tells me that the exception is uncaught by user code.

For example, imagine in the example below that the Title property setter on the object referenced by the "target" variable throws an ArgumentException.

Looking at the call stack, it seems that there is unmanaged code between the snippet below and the setter.

Can somebody please (& thank you!) explain:

  • Why is this happening in the first place?
  • Is there a simple way to fix it without re-thinking the program logic?

Here is my code:

try
{
    prop.SetValue(target, attr.Value); // target does have a "Title" property
                                       // attr.Value == "Title"
                                       // the setter throws an ArgumentException

}
catch (Exception ex) // No exception is ever caught.
{
    errors.Add(ex.Message);
}

Here is the code for one of many properties that I want to set like this: public string Title { get { return this.title; }

        set
        {
            if (string.IsNullOrEmpty(value) || value.Length < 1 || value.Length > 128)
            {
                throw new ArgumentException("Title must be at least 1 character and cannot be longer than 128 characters.");
            }

            this.title = value;
        }
    }
  • 3
    show this Title property code then... – T McKeown Jun 25 '14 at 20:43
  • 2
    A SSCCE would help. http://sscce.org/ – L.B Jun 25 '14 at 20:43
  • 6
    Also, you should not assume that `InnerException` is populated. It may be `null`. Use `ex.ToString()` to get "everything the exception type wants you to see, including inner exceptions" – John Saunders Jun 25 '14 at 20:44
  • Please explain why you want to use reflection here – Mare Infinitus Jun 25 '14 at 20:49
  • @MareInfinitus I'm not sure that's relevant. OP asked for a way to "fix it without re-thinking program logic" – Michael Jun 25 '14 at 20:51
  • I'm of the strong opion that if it requires reflection, something else is wrong. There are reasons for reflection, but I want to know if they are given here. – Mare Infinitus Jun 25 '14 at 20:54
  • Is it possible you are confusing an "uncaught" exception with a ["first chance exception"](http://blogs.msdn.com/b/davidklinems/archive/2005/07/12/438061.aspx)? – Chris Sinclair Jun 25 '14 at 20:59
  • @MareInfinitus So you can tell OP to rethink the program's logic? "You're doing it wrong, and I won't help you until I know why" doesn't foster a sense of community. – Michael Jun 25 '14 at 21:00
  • 1
    You're sure it's not simply the debugger set to halt on all exceptions regardless of whether they are user-handled? What IS the ArgumentException telling you? – McAden Jun 25 '14 at 21:07
  • @Mick No, the only thing is that I need more information. Please do not Machiavelli-ize my comment – Mare Infinitus Jun 25 '14 at 21:16
  • @McAden I had Visual Studio set to halt on all "user-unhandled" exceptions. I was catching the exception, just not in the method where it was thrown. Sorry, I don't have enough reputation to up-vote you... – user3776518 Jun 30 '14 at 17:08
  • @user3776518 - you should be able to accept answer below. Glad you were able to resolve it. – McAden Jun 30 '14 at 17:33

2 Answers2

1

EDIT as stated by @Default, Framework 4.5 does have an overload with only two parameters, so if the user is working with FW 4.5 this answer does not have relevance (at least the last part about PropertyInfo),

You are wrong, it is trapped and here is an example to demonstrate it:

public class ExceptionGenerator
{
    public static void Do()
    {

        ClassToSet clas = new ClassToSet();

        Type t = clas.GetType();

        PropertyInfo pInfo = t.GetProperty("Title");

        try
        {

            pInfo.SetValue(clas, "test", null);
        }
        catch (Exception Ex)
        {

            Debug.Print("Trapped");

        }
    }
}

class ClassToSet
{

    public string Title {

        set {

            throw new ArgumentException();

        }

    }

}

What you are doing wrong is obtaining the PropertyInfo, the PropertiInfo's SetValue method expects a third parameter, the index at the property (null in your case), so your "prop" is not a PropertyInfo, I assume it's a FieldInfo, and because that it throws an unhandled exception.

Gusman
  • 14,905
  • 2
  • 34
  • 50
  • PropertyInfo definitely has a .SetValue() overload with 2 parameters – Michael Jun 25 '14 at 20:54
  • [this SetValue](http://msdn.microsoft.com/en-us/library/hh194291%28v=vs.110%29.aspx) takes two arguments. – default Jun 25 '14 at 20:54
  • That's only on FW 4.5... I'm still with FW 4.0 :S, but if the user is not using the FW 4.5 then the answer is correct. – Gusman Jun 25 '14 at 20:55
0

Any exception there should be caught.

See fiddle: https://dotnetfiddle.net/koUv4j

This includes errors in the reflection call itself (setting the property to the wrong Type), or having an exception within the property's setter itself (set throws).

This leads to something else being wrong. Possibilities:

  • You've got your IDE set to halt on all exceptions
  • The exception isn't happening where you think it is (like, in the catch, which will rethrow)

If it's not one of those 2 then please provide more information.

McAden
  • 13,714
  • 5
  • 37
  • 63