This project has moved and is read-only. For the latest updates, please go here.
1

Closed

SimpleIoc not re-throwing exceptions thrown within constructors

description

Hello,

I've come across a problem with SimpleIoc. It doesn't seem to re-throw exceptions that occur within ViewModel constructors when resolving a ViewModel within the ViewModelLocator.

The code example below is taken directly from this Stack Overflow question:
public partial class App : Application
{
        public App()
        {
           this.Dispatcher.UnhandledException += OnDispatcherUnhandledException;
        }

        void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
           string errorMessage = string.Format("An unhandled exception occurred: {0}", e.Exception.Message);
           MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
           e.Handled = true;
        }
}
public class TestViewModel : ViewModelBase
{
    public TestViewModel()
    {
        throw new Exception("this exception will be catched by SimpleIoC, therefore i'm not able to handle it elsewhere");
    }
}
// I'm using the MVVMLight ViewModelLocator which looks like this:

public class ViewModelLocator
{
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        SimpleIoc.Default.Register<TestViewModel>();
    }

    public TestViewModelTestViewModel
    {
        get
        {
            return ServiceLocator.Current.GetInstance<TestViewModel>();
        }
    }

    public static void Cleanup()
    {
        SimpleIoc.Default.Unregister<TestViewModel>();
    }
}
I've attached a simple example project which I just created using the 5.0.2.0 of MVVM Light (via Nuget) using Visual Studio 2010. You can debug the project to see the problem or just run the EXE in the bin folder. The unhandled exception handler in App.xaml.cs should catch the exception but doesn't.

Please let me know if you need any more info.

Kind regards,

Phil

file attachments

Closed Nov 26, 2017 at 10:23 AM by lbugnion
Cannot reproduce. What I see is that the call to GetInstance throws the exception as expected.

comments