Announcement Announcement Module
Collapse
No announcement yet.
ExceptionHandler on method level. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ExceptionHandler on method level.

    Good Morning,

    Is it possible to someone apply an exception handler to just a method rather than a full class?

    My controller class has Ajax and non-Ajax methods. The former which return JSON responses and the latter which return a view.

    I want to be able to catch exceptions implicitly on the Ajax methods and return a JSON response from the error seperately from the non-Ajax methods returning to an error view.

    To get arround this I need to wrap my full ajax method in a try/catch for Throwable which is additional boiler plate I do not need.

    It seems that it is a functional let down that you can only apply to a full class.

    Thanks

    Chris

  • #2
    Hello

    Is it possible to someone apply an exception handler to just a method rather than a full class?
    What do you mean?, try/catch/finally are written inside a method, expand your idea, I am confused

    I want to be able to catch exceptions implicitly on the Ajax methods and return a JSON response from the error seperately from the non-Ajax methods returning to an error view.
    If you can post your code for both approaches would be nice to get a better idea

    To get arround this I need to wrap my full ajax method in a try/catch for Throwable which is additional boiler plate I do not need.
    You could consider AOP

    Comment


    • #3
      In a Controller class I can do the following for ajax calls:

      Code:
          @ExceptionHandler(Exception.class)
          public @ResponseBody void handleException(Exception ex, HttpServletRequest request) {
              // Build a JSON representation of the exception.   
              String response = jsonUtil.getExceptionJson(ex);
              // Write JSON response to the response object
          }
      
          @RequestMapping(value = "/doAjax", method = RequestMethod.POST)
          public @ResponseBody Map<String,String> doAjax(params omitted) {
              Map<String, String> map = new HashMap<String, String>();
              // Do business methods which can throw exceptions and build map.
              return map;
          }
      or the following for non ajax.

      Code:
          @ExceptionHandler(Exception.class)
          public String handleException(Exception ex, HttpServletRequest request) {
             return "exceptionView";
          }
      
          @RequestMapping(value = "/doNonAjax", method = RequestMethod.POST)
          public String doNonAjax(params omitted) {
              // Show completed view
              // Do business methods which can throw exceptions and add to model.
              return "completedView";
          }
      However as the ExceptionHandler method is applied to a full class then you can not mix and match ajax and non ajax methods as the exception handler either needs to either return a view OR a response body.

      To get around this I need to do the following with a mixed class:

      Code:
          @ExceptionHandler(Exception.class)
          public String handleException(Exception ex, HttpServletRequest request) {
             return "exceptionView";
          }
      
          @RequestMapping(value = "/doAjax", method = RequestMethod.POST)
          public @ResponseBody Map<String,String> doAjax(params omitted) {
              try {
                  Map<String, String> map = new HashMap<String, String>();
                  // Do business methods which can throw exceptions and build map.
                  return map;
              }
              catch(Exception e) {
                  String exceptionJson = jsonUtil.getExceptionJson(ex);
                  // Add exceptionJson to the response map.   
              }
          }
      
          @RequestMapping(value = "/doNonAjax", method = RequestMethod.POST)
          public String doNonAjax(params omitted) {
              // Show completed view
              // Do business methods which can throw exceptions and add to model.
              return "completedView";
          }
      That is a crude example to show what I mean. What I am getting at is it seems a functional downfall that a Single method can not have its own ExceptionHandler set like.

      @RequestMapping(value = "/doNonAjax", method = RequestMethod.POST, exceptionHandler = "someHandler")
      public String doNonAjax(params omitted)

      @RequestMapping(value = "/doAjax", method = RequestMethod.POST, exceptionHandler = "someOtherHandler")
      public @ResponseBody Map<String, String> doAjax(params omitted)

      Chris

      Comment


      • #4
        Does anyone know how to achive exceptionhandling functionallity on method level using @ExceptionHandler?

        Comment


        • #5
          Hello

          Originally posted by hasle View Post
          Does anyone know how to achive exceptionhandling functionallity on method level using @ExceptionHandler?
          Have you read the Reference documentation?

          16. Web MVC framework

          You can start reading this section: 16.11.2 @ExceptionHandler

          Comment


          • #6
            From what I see the @ExceptionHandler is on the controller level and not method

            Comment


            • #7
              Have you read the Reference documentation?

              16. Web MVC framework

              You can start reading this section: 16.11.2 @ExceptionHandler
              This is not a very helpful post and doesn't answer anything regarding his problem.


              Originally posted by hasle View Post
              From what I see the @ExceptionHandler is on the controller level and not method
              I agree with you, and I am having the same problem as you.

              Can anyone suggest a way to have two methods (methodA and methodB) on a controller, both of which could throw an exception "MyExampleExeption".

              Then two exception handler methods, one which handles any "MyExampleException" thrown from methodA, and another one which handles any "MyExampleException" thrown from methodB.

              As per given previous example supplied, something like the following would be helpful if it existed:
              Code:
              @RequestMapping(value = "/doMethodA", method = RequestMethod.POST, exceptionHandler = "exampleHandler1")
              public String methodA(String value)
              
              @RequestMapping(value = "/doMethodB", method = RequestMethod.POST, exceptionHandler = "exampleHandler2")
              public @ResponseBody MyObject methodB(String value)
              Any help or suggestions would be appreciated.

              Comment


              • #8
                This is not a very helpful post and doesn't answer anything regarding his problem.
                What is the problem with 16.11.2 @ExceptionHandler? There is an example showing the follow

                Code:
                @Controller
                public class SimpleController {
                
                  // other controller method omitted
                
                  @ExceptionHandler(IOException.class)
                  public String handleIOException(IOException ex, HttpServletRequest request) {
                    return ClassUtils.getShortName(ex.getClass());
                  }
                }
                You could use such annotation for some customized method within your controller y forgot your exceptionHandler = "exampleHandler1"

                Comment


                • #9
                  Thank you for your response, however I have read the documentation previously and already have working @ExceptionHandler methods in my controllers, however it still doesn't solve my specific problem as per above posts. I have resorted to using a try catch in each method that needs specific exception behaviour, and for the methods where that same exception can use a more general/catchall exception behaviour, I will let my @ExceptionHandler method handle it.

                  Comment

                  Working...
                  X