Global Exception Handling in MVVM

Is there a way to implement global exception handling with the MVVM pattern. In my existing case, whenever an error occurs inside the ViewModel, the application does not crash, it simply “hides” the other bindings that occur after the code that caused the error (of course, this is very misleading for the end user, not true, and this never happens should not happen). I would not want to implement try catch for each operation in the viewModel, and I don’t like the hidden way of eliminating errors, I really would like to implement a way to use a WPF application to handle global errors. Is there any way to do this with MVVM?

+4
source share
3 answers

After a long battle, finally, I found a very simple way to implement exception handling inside the ViewModel. When creating a BindingListener that inherits from DefaultTraceListener, there is certainly a great way to find your binding errors during debug mode, it will not catch exceptions that occurred inside the ViewModel when the running solution is standard. But AppDomain.CurrentDomain.FirstChanceException will be.

App.xaml.cs:

AppDomain.CurrentDomain.FirstChanceException += new EventHandler<System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs>(CurrentDomain_FirstChanceException);


    private void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
    {
            Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("Error Occurred \n\r" + e.Exception.Message + "\n\r" + e.Exception.StackTrace, "ERROR", MessageBoxButton.OK, MessageBoxImage.Error)));
    }
+1
source

Look at the class output DefaultTraceListener. I saw how people extract their own from it BindingListenerand redefine the method WriteLineto throw their own exceptions.

You can simply unscrew one of them during the launch of the application, and it should go further:

public class BindingListener : DefaultTraceListener`
{
    public BindingListener()
    {
        PresentationTraceSources.Refresh();
        PresentationTraceSources.DataBindingSource.Listeners.Add(this);SourceLevels.Error;
    }
    public override void WriteLine(string message){...}
}

. , , , .

0

lamba. - ...

        public async void DoSomething()
        {
            await RunSafe(async () =>
            {
                await model.DoSomething();
                await model.DoSomethingElse();
                await model.DoLastThing();
            });
        }
        private async Task RunSafe(Func<Task> del, [CallerMemberName] String methodName = "")
        {
            try
            {
                Log.Info("Executing {0}", methodName);
                await del();
            }
            catch (Exception ex)
            {
                StatusMessage = string.Format("Error in {0}(...): {1}\r\n{2}", methodName, ex.Message, ex.ToString());                

                Log.Error("Error occured in plug in.", ex);
            }
        }
0

Source: https://habr.com/ru/post/1657813/


All Articles