Announcement Announcement Module
Collapse
No announcement yet.
AOP Exception handling (unchecked v. checked exceptions) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP Exception handling (unchecked v. checked exceptions)

    I am planning on using the ThrowsAdvice AOP interceptor in my own code. It seems that I am restricted to using unchecked exceptions in my code now if I plan to only catch them with Spring's AOP interceptor (and not catch them in the method that might throw them).

    If the exception is checked, the compiler will complain that I do not necessarily catch it in the method being intercepted, and calling methods will now have to deal with these exceptions.

    Are there any ways around this that I am missing, or will I just need to document clearly that I am using unchecked exceptions where I am using this interceptor? Thanks!

  • #2
    You are correct in what you are saying. If you use checked exceptions then the compiler will complain that you are not catching them, even though you are in your throws advice - after all it doesn't know about your advice .

    If the exceptions you have are unrecoverable anyway then unchecked exceptions would be the way to go from a design point of view, similar to what we have in Spring. If they are recoverable then you will probably want to catch them in app code anyway - so you can recover.

    If you really want to catch all exceptions, both checked and unchecked, in an advice and not have the compiler complain, then you can use the exception softening feature of AspectJ.

    Rob

    Comment


    • #3
      Re: AOP Exception handling (unchecked v. checked exceptions)

      You can define your aspect throws handlers as:
      Code:
      afterThrowing([Method], [args], [target], Throwable subclass) throws YourCheckedException_OR_java_lang_Exception
      As these methods will be called via Reflection the exceptions you throw in this method will be rethrown by the AOP_Proxy.
      If you just return from the handler method, the original exception will be rethrown.

      See org.springframework.aop.framework.adapter.ThrowsAd viceInterceptor.

      Edit: Pay attention, that throwing a checked exception, that is not declared in the target method will be wrapped by an UndeclaredThrowableException(extends RuntimeException).

      Comment

      Working...
      X