Best practice for handling error

Jan 24, 2012 at 3:07 PM

Hello,

in my WPF application I've got a lot o LoadData functions, each one do exacly the same, retrieve data and put it into a gridview.. for what concern the error for now I show a MessageBox with the error, so my code is in the form

 

return CoroutineFns.AsResult(
                    () =>
                    _repository.function(Date, results =>
                    {
                        DataItems = new BindableCollection<myResult>(results.Cast<myResult>());
                    },
                    e =>
                    {
                    error = e; //log exception

                    }));

 

What's the best practice for logging all the exception in a single point?

Thanks

Jan 24, 2012 at 9:02 PM

Generally you would create a shared ErrorHandler class, with an IErrorHandler interface and then inject it into your ViewModels. Whenever an error occurs, you call the HandleError method and pass it the exception.

    public interface IErrorHandler
    {
        void HandleError(Exception ex);
    }
    [Export(typeof(IErrorHandler))]
    public class ErrorHandler : IErrorHandler
    {
     
        #region IErrorHandler Members
 
        public void HandleError(Exception ex)
        {
            // log error and display error message
        }
 
        #endregion
    }