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 913023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:29:01+00:00 2026-05-15T17:29:01+00:00

Using Spring 3, I like to create an exception handler using the ExceptionHandler annotation

  • 0

Using Spring 3, I like to create an exception handler using the ExceptionHandler annotation that will handle “no page found (404)” requests. I am using the following code to do this. But when I point to a URL that does not exist, the default exception handler defined by Spring is being invoked.

It might be that I’m handling the NoSuchRequestHandlingMethodException exception. If it is, then what exception should I be registering?

Will you please take a look at the following code and see what I’m doing wrong?

NOTE: If I change the exception in the @ExceptionHandler to NullPointerException and create a RequestMapping to throw null pointer, that will work.

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.web.servlet.ModelAndView;

    @Controller
    public class GeneralHandler {
      private final Log logger = LogFactory.getLog(getClass());

      @ExceptionHandler(NoSuchRequestHandlingMethodException.class)
      public ModelAndView handleException (NoSuchRequestHandlingMethodException ex) {
        ModelAndView mav = new ModelAndView();
        logger.error("Exception found: " + ex);
        return mav;
      }
    }
  • 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-15T17:29:02+00:00Added an answer on May 15, 2026 at 5:29 pm

    @ExceptionHandler-annotated methods are invoked when a @RequestMapping method on that same class throws an exception. So when you added the mapping which threw the NullPointerException, that worked, since the mapped method and exception handler were together in the same class.

    When no mapping is found, Spring has no way of associating the NoSuchRequestHandlingMethodException with your @ExceptionHandler, because it didn’t get as far as matching the request to a handler method. This isn’t mentioned explicitly in the docs, but is the behaviour I’ve observed.

    If you want to handle this exception specially, you’re going to have to use the more general HandlerExceptionResolver approach, rather than the more specialised @ExceptionHandler technique.

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

Sidebar

Related Questions

I'm using Spring MVC and jquery autocomplete, my spring controller looks like this @RequestMapping(value
I am using spring 3.0.2 for developing a simple web based application like twitter.
I am using Spring Security 3.0.6. for modularity reasons, I would like to use
I'd like to run my Spring application using STS, but I have no idea
In layout xml file, I set properties using @string like this: <com.hardpass.MyCustomComponent android:layout_width=fill_parent android:layout_height=wrap_content
I have string like \LESSING\root\cimv2:Win32_UserAccount.Domain=LESSING,Name=Admin How to convert it to LESSING\Admin using Framework?
I am converting a string like 41.00027357629127, and I am using; Convert.ToSingle(41.00027357629127); or float.Parse(41.00027357629127);
Why can a string like {opacity: 1.0, width: '132px'} not be evaluated using eval()
When using SQL Server Express 2005's User Instance feature with a connection string like
I have a string like so: option_alpha=value option_beta=some other value option_gamma=X ...etc. I'm using

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.