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

  • Home
  • SEARCH
  • 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 6337551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:16:22+00:00 2026-05-24T19:16:22+00:00

I am attempting to set-up a scheme for uniformly handling exceptions in Spring. As

  • 0

I am attempting to set-up a scheme for uniformly handling exceptions in Spring. As such, I need a way to pass context information into an @ExceptionHandler-annotated method, for example consider the following:

@ExceptionHandler(Exception.class)
public void handleException(Exception ex, HttpServletRequest request) {
    // Need access to myContext from login()
}

@RequestMapping(value = "{version}/login", method = RequestMethod.POST)
public void login(HttpServletRequest request, @PathVariable String version, @RequestParam("userName") String userName, @RequestParam("password") String password, ModelMap model) throws Exception {
    ...
    myContext = "Some contextual information"
    ...
    i_will_always_throw_an_exception();
}

Since Spring is responsible for translating a thrown exception into an invocation of handleException(), I am having difficulty trying to find a way to pass myContext to the handler. One thought I have is creating a subclass of HttpServletRequest. If that approach works I would have code like this:

@ExceptionHandler(Exception.class)
public void handleException(Exception ex, MyCustomHttpServletRequest request) {
    // I now have access to the context via the following
    String myContext = request.getContext();
}

@RequestMapping(value = "{version}/login", method = RequestMethod.POST)
public void login(MyCustomHttpServletRequest request, @PathVariable String version, @RequestParam("userName") String userName, @RequestParam("password") String password, ModelMap model) throws Exception {
    ...
    myContext = "Some contextual information"
    request.setContext(myContext);
    ...
    i_will_always_throw_an_exception();
}

But, if I follow this approach, how do I properly use my own arbitrary sub-class of HttpServletRequest to make this work?

  • 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-24T19:16:24+00:00Added an answer on May 24, 2026 at 7:16 pm

    Can’t you just put it into exception (if necessary – wrapping the original exception with the new one)?

    @ExceptionHandler(MyContextualException.class)
    public void handleException(MyContextualException ex) {
        // Need access to myContext from login()
    }
    
    @RequestMapping(value = "{version}/login", method = RequestMethod.POST)
    public void login(HttpServletRequest request, @PathVariable String version, @RequestParam("userName") String userName, @RequestParam("password") String password, ModelMap model) throws Exception {
        ...
        myContext = "Some contextual information"
        ...
        try {
            i_will_always_throw_an_exception();
        } catch (Exception ex) {
            throw new MyContextualException(myContext, ex);
        }
    }
    

    An alternative approach is to pass the context as a request attribute:

    request.setAttribute("myContext", myContext);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to set an asp.net textbox to a SQL 2005 money data
I'm attempting to set up a symbol proxy using symproxy.dll from Debugging Tools for
I am attempting to set a value in a textarea field using jquery with
I am attempting to set up my own project using a third party library
I have a Perl script that I'm attempting to set up using Perl Threads
Possible Duplicate: C# generic constraint for only integers Greets! I'm attempting to set up
I am attempting to run the following command in KornShell (ksh): set -A INDEXES
I'm attempting to use Dapper to return a set of Shares and an associated
I am attempting to use java scanner with the following set of delimiters &
I am attempting to use the code-behind (Page_Load or PreRender) to set a date-time

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.