user278618
user278618

Reputation: 20262

Exception not showing

I have :

 public class Person : INotifyPropertyChanged
    {
       private string _name;

        public int Age { get; set; }

        public string Name
        {
            get { return _name; }
            set
            {
                if (!String.IsNullOrEmpty(_name))
                {
                    if (String.IsNullOrEmpty(value))
                    { 
                       throw new Exception("name couldn't be null");



                    }
                    else if ((_name.Equals(value) != true))
                    {
                        if (!String.IsNullOrEmpty(value))
                        {
                            throw new Exception("name couldn't be null");
                        }
                        else
                        {
                            InvokePropertyChanged("_name");
                        }


                        _name = value;
                    }

                }

                else if (String.IsNullOrEmpty(value))
                {
                    throw new Exception("name couldn't be null");
                }
                else
                {
                    _name = value;
                }

            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void InvokePropertyChanged(string propertyName)
        {

            var e = new PropertyChangedEventArgs(propertyName);

            PropertyChangedEventHandler changed = PropertyChanged;

            if (changed != null) changed(this, e);

        }

>
<Grid>
    <StackPanel>
        <TextBox Name="tbName" Text="{Binding Path=Name, Mode=TwoWay}"></TextBox>
        <TextBox Name="tbOther" Text="Come in"></TextBox>
    </StackPanel>
</Grid>

and

 public UserControl1()
        {
            InitializeComponent();

            Person person = new Person();
            person.Name = "Patrick";

 this.DataContext = person;

        }

Why when I debug and enter to line of :

throw new Exception("name couldn't be null");

exception is not showing.

Upvotes: 0

Views: 2083

Answers (4)

Prashant
Prashant

Reputation: 937

try doing this:

...
else if (String.IsNullOrEmpty(value))
{
    try{
        throw new Exception("name couldn't be null");
    }
    catch(Exception ex)
    {
         //Set Breakpoint Below
         int x=0;
    }
}
...

You will see that your code will come in the catch block, and the exception does get thrown. The handling of the exception however is upto you. The best way is already suggested by Quartermeister.

Otherwise based on your VS IDE settings, the exception gets suppressed, and you do not see an error on screen.

Upvotes: 1

CodeNaked
CodeNaked

Reputation: 41393

Please see this other question, but basically the exception will be handled and suppressed by the Binding.

Upvotes: 2

Quartermeister
Quartermeister

Reputation: 59139

I think you want to assign the Person object to the control's DataContext:

public UserControl1()
{
    InitializeComponent();
    Person person = new Person();
    person.Name = "Patrick";
    this.DataContext = person;
}

You may also want to set ValidatesOnExceptions on your binding so that the UI will display the error template when an exception is thrown in the setter.

<TextBox Name="tbName" Text="{Binding Path=Name, Mode=TwoWay, ValidatesOnExceptions=True}"></TextBox>

Upvotes: 1

JustLoren
JustLoren

Reputation: 3234

You're not setting the control to be bound to your person object from what I can see. As such, it shouldn't be attempting to set it and subsequently erroring.

When you put a break point in your getter, does it even get called?

Upvotes: 1

Related Questions