1

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?

2 Answers 2

1

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.

0

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

Unfortunately, yes, you must do this.

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

Some people say yes. You don't have to use lambdas. You can use ordinary static or instance methods.

To avoid delegates, you could change the design. When a user interface event occurs, you can use a general purpose code with a an "action" identifier, such as a Command object with a unique type or name, to execute code elsewhere (command processors) that uses try/catch blocks in a generic way, instead of putting such blocks within all event handlers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.