C# – Using Statement lambda in exception handling

cexception handlinglambdamvpnet

Following is a code snippet from MVP Win Forms application and this explanation would be helpful when answering the questions.

My DAL doesn't handle exceptions and it will be propagated up to the calling method in the presenter classes where the exception will be handled.

I'm using a single handler called ExecutAction(Action action) so I'm catching exceptions in one place rather than repeating code in every method.

At the moment, I'm not logging errors. Just alert the user for an action and try to keep the system alive if possible.

When showing messages to users, Presenters will use a static class called MessagingService. (ShowErrorMessage()). So that I can customize all massage boxes in one place.

        private void Search()
        {
            ExecutAction(() =>
            {
                var info = _DataService.GetByACNo(_model.AccountNumber);

                    if (info != null)
                    {
                        _Model = info ;
                        this.SetViewPropertiesFromModel(_Model, _View);
                    }
                    else
                    {
                        MessageBox.Show ("Bank account not found");
                    }
                });
            }


            private void ExecutAction(Action action)
            {
                try
                {
                    action();
                }

                catch (NullReferenceException e) { MessagingService.ShowErrorMessage(e.Message); }
                catch (System.Data.SqlTypes.SqlTypeException e) { MessagingService.ShowErrorMessage(e.Message); }
                catch (System.Data.SqlClient.SqlException e) { MessagingService.ShowErrorMessage(e.Message); }
            }
        }

Should I include general exception handler to this, to be able to handle any unforeseen exceptions?

Also could you show me a better way to handle showing messages than using a static?

Does use of lambda statements in every method call (ExecutAction(() =>) degrade code readability?

When showing user messages how to show a custom message like "Check the server connection" etc. first and then if the user wants more information (like StackTrace / technical details) he /she could press a button like More Info which is in the MessageBox dialog?

Best Answer

First of all, you should copy the reference of action delegate instance before invoking it to prevent threading issues, like this:

private void ExecutionAction(Action action)
{
    var handler = action;
    if (handler == null)
    {
        return;
    }

    try
    {
        handler();    
    }
    catch (System.Data.SqlTypes.SqlTypeException e)
    {
        MessagingService.ShowErrorMessage(e.Message);
    }
    catch (System.Data.SqlClient.SqlException e) 
    { 
        MessagingService.ShowErrorMessage(e.Message);
    }
}

Secondly, you shouldn't be catching NullReferenceExceptions. NREs almost always indicate a programming bug and it is better to fail fast instead of showing an error message without rethrowing it.

Related Topic