Screen swallowing exception in DisplayName

Topics: Getting Started, UI Architecture
Dec 12, 2012 at 5:00 PM
Edited Dec 12, 2012 at 5:02 PM

Good day,

I've stumbled across a problem.

If, for example, there is some sort of exception in the DisplayName property getter for a Caliburn.Micro.Screen, no exception bubbles up throw the framework when starting up an WPF application (which displays the MainView/MainViewModel).

 

    public class MainViewModel : Screen
    {
        public MainViewModel()
        {
        }

        public override string DisplayName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                base.DisplayName = value;
            }
        }
    }

MainView.xaml is a <Window> that contains an empty grid

 

If I change the class to manually implement IScreen, the exception bubbles up as expected.

 

Is there any way to have these exceptions bubble up through the framework?

 

Thanks,

James

Dec 12, 2012 at 5:22 PM

Generally you should avoid throwing exception in property getters.

The framework does not care about this property. It is only used by Xaml Binding.
WPF is catching binding exceptions so you wont see them.

Dec 12, 2012 at 6:49 PM

tibel,

No disagreement that I should avoid throwing exceptions in getters.  

The actual exception I introduced was not intentional and I ended up spending extra time when things did not occur as expected.

 

Following the stack overflow link in your response, I was able to use information from 

http://www.jasonbock.net/jb/Default.aspx?blog=entry.0f221e047de740ee90722b248933a28d

and

http://stackoverflow.com/a/11632148/550075

in order to have binding errors be thrown as exceptions.

 

Thanks for your quick response,

James