Mediator
Mediator

Reputation: 15378

How to protect yourself from argumentOutOfRangeException

I have numericUpDown control minValue - 0 maxValue - 100.

I create binding to this control.

If the value changes to 101 will be the exception, which I do not need, and I would like to value is not specifically mentioned. how to do it ?

UPDATE:

BindinHelper.BindField(this.nUpDownExecArea, "Value", TempConfigClass, "ExecArea");

BindField:

public static void BindField(Control control, string propertyName,
               object dataSource, string dataMember)
        {
            Binding bd;

            for (int index = control.DataBindings.Count - 1; (index == 0); index--)
            {
                bd = control.DataBindings[index];
                if (bd.PropertyName == propertyName)
                    control.DataBindings.Remove(bd);
            }
            control.DataBindings.Add(propertyName, dataSource, dataMember, false, DataSourceUpdateMode.OnPropertyChanged);
        }

I set TempConfigClass.ExecArea = 99999;

does not result in errors, but when I go to a tab (tabcontrol) where the error appears to be numericUpDown

Upvotes: 0

Views: 865

Answers (1)

m.edmondson
m.edmondson

Reputation: 30892

An argumentOutOfRangeException is telling you that the argument received was exceptional. However if your app is designed in a way to expect certain arguments there are two ways to go about it:

The recommended way - Simply check the value prior to the binding taking place and prevent it going any further if out of range

or - Use a try/catch block to catch the specific exception only and deal with it accordingly

Upvotes: 3

Related Questions