Announcement Announcement Module
Collapse
No announcement yet.
@ExceptionHandler with multiple exception types Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @ExceptionHandler with multiple exception types

    Just wondering if there is a way to catch multiple, non-related exception types with the @ExceptionHandler annotation.

    For example, I would like to avoid copying roughly the same method just to annotate it with two different exception types:


    @ExceptionHandler(UserNotFoundException.class)
    public ModelAndView securityRedirect(HttpSession session){
    UserBean userBean = (UserBean) session.getAttribute("userBean");
    if(userBean != null){
    if(log.isInfoEnabled())log.info("Retrieve response: User["+ userBean.getUserName() +"/"+ userBean.getPassword() +"]");
    }
    session.removeAttribute("userBean");
    session.invalidate();
    ModelAndView mav = new ModelAndView("sessionExpired");
    return mav;
    }

    @ExceptionHandler(ApplicationException.class)
    public ModelAndView errorRedirect(HttpSession session){
    UserBean userBean = (UserBean) session.getAttribute("userBean");
    if(userBean != null){
    if(log.isInfoEnabled())log.info("Retrieve response: User["+ userBean.getUserName() +"/"+ userBean.getPassword() +"]");
    }
    session.removeAttribute("userBean");
    session.invalidate();
    ModelAndView mav = new ModelAndView("sessionExpired");
    return mav;
    }


    Thanks in advance.

  • #2
    From the official documentation, page 455:

    The @ExceptionHandler value can be set to an array of Exception types. If an exception is thrown
    matches one of the types in the list, then the method annotated with the matching
    @ExceptionHandler will be invoked. If the annotation value is not set then the exception types listed
    as method arguments are used.

    Comment

    Working...
    X