Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6088729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:01:08+00:00 2026-05-23T12:01:08+00:00

My Spring MVC app is full of methods that look like this: @RequestMapping(value =

  • 0

My Spring MVC app is full of methods that look like this:

@RequestMapping(value = "/foo", method = RequestMethod.GET)
public final void foo(HttpServletRequest request, ModelMap modelMap){
    try{
        this.fooService.foo();
    }
    catch (Exception e){
        log.warn(e.getMessage(), e);
    }
}

Exceptions are caught and logged but not handled otherwise.

The fooService called above does the same thing, never throwing exceptions up to the controller but catching and logging them. So, actually this controller exception code will never get invoked.

What’s the best and simplest approach to implement proper exception handling in my app?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:01:09+00:00Added an answer on May 23, 2026 at 12:01 pm

    Get rid of all catch statements if all they do is logging carelessly. catch is meant to handle the error, not hide it.

    Once all these catches are removed, install one global exception resolver in Spring MVC (1, 2, 3, …) Simply implement this trivial interface:

    public interface HandlerExceptionResolver {
        ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);
    }
    

    In your exception resolver you might simply log the exception once and let it go as unprocessed (return null), so that error mappings in web.xml will forward request to proper error page. Or you can handle exception yourself and render some error page. AFAIK in simplest case there is no need for register exception resolver, just define it as a Spring bean/annotate with @Service.

    Remember, catch the exception only when you know what to do with. Logging is only for troubleshooting, it doesn’t handle anything.

    BTW this:

    log.warn(e.getMessage(), e);
    

    is not only a very poor exception handling, but it is also slightly incorrect. If your exception does not have a message, you will see mysterious null just before the stack trace. If it does, the message will appear twice (tested with Logback):

    22:51:23.985 WARN [main][Foo] OMG! - this is the exception message
    java.lang.IllegalStateException: OMG! - this is the exception message
        at Foo.bar(Foo.java:20) ~[test-classes/:na]
    

    …sometimes undesirable, especially when exception message is very long.


    UPDATE: When writing your own exception logger consider implementing both org.springframework.web.servlet.HandlerExceptionResolver and org.springframework.core.Ordered. The getOrder() should return something small (like 0) so that your handler takes precedence over built-in handlers.

    It just happened to me that org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver running prior to my handler returned HTTP 500 without logging the exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to use Websockets with a Spring MVC-powered web app. I've read o
I have an asp.net mvc project that uses some search methods in a seperate
I am writing an asp.net MVC app that drives an IPhone application. I want
I have an MVC app that has Controller that takes a IDomainService . The
I have a MVC app that is loading a external DLL and when in
I have a very simple MVC app at this point: Controller: public class HomeController
I'm using the WebBrowser control in an ASP.NET MVC 2 app (don't judge, I'm
I have this MVC form with 2 dropdownlist where selection on the first dropdown
I'm currently building a Spring MVC application. I was looking to use JSP pages
I am playing with a little web-app that is planned to have a presentation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.