0

I'm writing a program following completely the MVC design pattern, currently I have lots of methods that throw exceptions and I'm handling them poorly.

I thought of adding a class in the controller called exceptionHandler, which would have methods like public void receiveRuntimeException(String message), receiveAnotherTypeOfException(...), etc.

Then giving most classes (particularly those in the view) a reference to exceptionHandler and then whenever a methods throws an exception, do something like this

try{
    methodThatWillThrowAwfulException()
}catch(AwfulException e){
    exceptionHandler.receiveAwfulException("methodThatWillThrowAwfulException threw awful exception")
}

Is this a good practice? If not, how should exceptions be handled in MVC?

csTroubled
  • 367
  • 1
  • 3
  • 9

2 Answers2

0

Spring has the concept of @ControllerAdvice, in which you can annotate a class to handle various aspects, such as error handling (thus annotated with @ExceptionHandler). You may not use Spring yourself; this is a pattern that is being adopted by a very popular MVC framework.

In that vein, you should strive to make as many methods as necessary to handle as many exceptions as necessary. Do not create one method to handle an omni-exception, since you want your errors to be as descriptive as possible.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Also take a look at: http://stackoverflow.com/questions/3551221/guidelines-on-exception-propagation-in-java?answertab=active#tab-top – Jaumzera Jun 15 '16 at 23:52
  • I don't quite understand that controller advice annotation, what's it supposed to do? Do you think that the option I showed in the question is alright? – csTroubled Jun 16 '16 at 02:35
0

You want to handling error? one controller. commonExceptionHandler?

(context.xml)

<!-- Exception Resolver -->
<beans:bean
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <beans:property name="defaultErrorView" value="exception/default"></beans:property>
    <beans:property name="warnLogCategory" value="log"></beans:property>
</beans:bean>

(web.xml)

<!-- error -->
<error-page>
    <error-code>301</error-code>
    <location>/exception/default.html</location>
</error-page>
<error-page>
    <error-code>304</error-code>
    <location>/exception/default.html</location>
</error-page>
<error-page>
    <error-code>307</error-code>
    <location>/exception/default.html</location>
</error-page>
<error-page>
    <error-code>401</error-code>
    <location>/exception/default.html</location>
</error-page>
<error-page>
    <error-code>402</error-code>
    <location>/exception/default.html</location>
</error-page>
<error-page>
    <error-code>403</error-code>
    <location>/exception/default.html</location>
</error-page>
<error-page>
    <error-code>404</error-code>
    <location>/exception/default.html</location>
</error-page>
<error-page>
    <error-code>405</error-code>
    <location>/exception/default.html</location>
</error-page>
<error-page>
    <error-code>406</error-code>
    <location>/exception/default.html</location>
</error-page>
<error-page>
    <error-code>415</error-code>
    <location>/exception/default.html</location>
</error-page>
<error-page>
    <error-code>429</error-code>
    <location>/exception/default.html</location>
</error-page>
<error-page>
    <error-code>500</error-code>
    <location>/exception/default.html</location>
</error-page>
<error-page>
    <error-code>501</error-code>
    <location>/exception/default.html</location>
</error-page>
<error-page>
    <error-code>503</error-code>
    <location>/exception/default.html</location>
</error-page>

(handler controller)

@ControllerAdvice("you.project.package")
public class CommonExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(CommonExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    public ModelAndView defaultException(HttpServletRequest req, Exception exception){
        logger.error("Request: " + req.getRequestURL() + " raised " + exception);

        ModelAndView mav = new ModelAndView();
        mav.setViewName("exception/default");

        return mav;
    }
}

You can to search in google.

Try. after You come to show error and code.

0gam
  • 1,343
  • 1
  • 9
  • 21