3 Replies - 500 Views - Last Post: 20 December 2010 - 08:29 AM Rate Topic: -----

#1 mouse88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 192
  • Joined: 27-July 10

Custom Control: Throwing Exceptions

Posted 19 December 2010 - 09:07 AM

I am creating a custom control and have the following property:

//Header shine opacity.
        private int headerShineOpacity = 50;
        /// <summary>
        /// Gets or sets the headers shine opacity. Value should be between 0 and 255.
        /// </summary>
        [Category("Colors"), Description("The headers shine opacity")]
        public int HeaderShineOpacity
        {
            get { return headerShineOpacity; }
            set 
            {
                if (value < 0 || value > 255)
                {
                    if (DesignMode == false)
                    {
                        throw new ArgumentOutOfRangeException("Opacity must be between 0 and 255");
                    }
                    else
                    {
                        MessageBox.Show("Header shine opacity must be a value between 0 and 255.", "Header Shine Opacity", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    headerShineOpacity = Clamp(value, 0, 255);
                    Invalidate();
                }
            }
        }



If I set up a test form and try to change the value to 500 at runtime it throws the exception but points to the code in my DLL. Ho can I get the excpetion to point the user to the line of code where they try to change the value?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Custom Control: Throwing Exceptions

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,360
  • Joined: 02-June 10

Re: Custom Control: Throwing Exceptions

Posted 19 December 2010 - 12:28 PM

If the custom control you are making is in a DLL, I don't think you can get it throw the exception someplace else.

You could try wrapping the setting of that value in a try/catch block. That should catch the exception at the point it was tried. But you have no way to force the user of your DLL into using a try/catch.

Instead - what if your control handles the problem more gracefully. Just raising the exception is rather crude.
If the user enters less than 0, then set the value *at* zero.
If the user enters more than 255, then set the value *at* 255.
Now there is no need for an exception message at all.
Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4312
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: Custom Control: Throwing Exceptions

Posted 20 December 2010 - 08:26 AM

View Postmouse88, on 19 December 2010 - 09:07 AM, said:

If I set up a test form and try to change the value to 500 at runtime it throws the exception but points to the code in my DLL. Ho can I get the excpetion to point the user to the line of code where they try to change the value?

Thanks


Likely you created a test project in the same solution, right? And you added a reference from the Projects tab in the Add Reference dialog?

It shows the exception in your DLL because it has loaded the debug symbols for that project. If you were to just build the project, copy the DLL into a different folder, and then make a new solution and add a reference to that DLL, the debugger would have no way of showing exceptions inside the DLL, and instead will show it as deep in the stack as it can, which is the part where it calls into the DLL.

Quote

Instead - what if your control handles the problem more gracefully. Just raising the exception is rather crude.
If the user enters less than 0, then set the value *at* zero.
If the user enters more than 255, then set the value *at* 255.
Now there is no need for an exception message at all.


I think that's mainly a point of style. I prefer my controls to do exactly what they're told, and fail if they cannot, rather than fixing their input to accepted values. Someone else using your code may not realize that the numbers are being changed for them, and could cause problems later. Granted, in this case, probably not, but in others, maybe.
Was This Post Helpful? 1
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Custom Control: Throwing Exceptions

Posted 20 December 2010 - 08:29 AM

Agree with tlh here, having it round off to the accepted value is the best way to go in this situation. +1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1