Results 1 to 3 of 3

Thread: AfterThrowingAdvice - trap and return something else?

  1. #1
    Join Date
    Dec 2011
    Posts
    9

    Default AfterThrowingAdvice - trap and return something else?

    If a method throws an exception and this is intercepted by an after-throwing advice can this trap the exception and instead propagate a method return response as per the original method?

    e.g.
    original method that can throw a IOException lets say:
    public
    Code:
    String getName() throws IOException {..}
    Advice:
    Code:
    public String processException(IOException e) {
                  LOG.exception (e);
                  return "NONAME";
               }
    If that is not possible can I throw a new type of exception?
    Last edited by pbarry30; Dec 15th, 2011 at 01:16 PM.

  2. #2
    Join Date
    Dec 2011
    Posts
    9

    Default

    I think the direct answer is no to the return type but yes to throwing a new type of exception.
    In AspectJAfterThrowingAdvice in the invoke method in the catch block, anything returned is ignored and the original exception is rethrown.

  3. #3
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    Use Around advice. In your advice code, wrap the pjp.proceed() call inside a try catch block, catching the type of exception that you expect. In the catch block, do logging stuff and then return what you want to return in case of exception.

    With AfterThrowing advice what you want to do is not possible.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •