Where should exception handling be performed to display to the system user?

13

What is the best place to handle an exception and send a message to the system user in a desktop application? In the controller, in the view or elsewhere?

    
asked by anonymous 11.12.2015 / 00:34

2 answers

12

In the controller.

Your model layer can produce exceptions if you try to perform some non-allowed operation or if the operation to be performed fails.

The controller then captures this error and decides how the view layer will show it.

The view layer has a responsibility, show the error, and perhaps capture a user action telling what to do. But it is not her responsibility to deal with the error.

Error handling is part of the application's flow control. And flow control is the controller's responsibility.

    
12.12.2015 / 12:12
7

First of all do not abuse catching exceptions. Make sure you can do something useful when capturing one and always catch the most specific exception possible.

Unfortunately, Java abuses exceptions to communicate with the various components. The language uses this mechanism for normal application flow.

That said, you may need to catch exceptions at any point.

During the execution of the model, exceptions may occur that need to be handled right there.

The same goes for visions, but it should be rare for a view to have any exceptions.

Because views typically should not have complex actions and are not directly related to the presentation, these manipulations will most often invoke a controller action.

Most of the exceptions may be intercepted in the controller since it will be common for the model to throw them. Remembering that a likely action when an exception is captured here will be the call of a view to inform the user.

Of course this depends on the form and most of which framework MVC that is working. Some of them have their own general handling modes for exceptions.

With few details I can not be more specific.

    
12.12.2015 / 13:03