Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
1.7.10
-
None
Description
When implementing a Category to handle exceptions, I find that the thrown exception is not available within the catch block when the class is annotated with the @Category AST transformation. Removing the @Category AST transformation gives the correct behaviour.
Example:
// Define exception handling category @Category(Object) class ExceptionHandler { def handled(Closure block) { try { block.call() } catch (Throwable t) { println t } } } // Define a class which mixes in this category @Mixin(ExceptionHandler) class Caller { def thrower() { handled { 1/0 } } } // Test the exception handling new Caller().thrower() // --> throws "ERROR groovy.lang.MissingPropertyException: No such property: t for class: Caller"
However, if the ExceptionHandler category is defined without using the @Category AST transformation:
class ExceptionHandler { static def handled(Object self, Closure block) { try { block.call() } catch (Throwable t) { println t } } } @Mixin(ExceptionHandler) class Caller { def thrower() { handled { 1/0 } } } new Caller().thrower() // --> prints "java.lang.ArithmeticException: Division by zero", as expected
This behaviour is observed both in the groovy shell, and in compiled code. Also, the erroneous behaviour is not due to the @Mixin AST transformation, as I observe the same behaviour when I replace the @Mixin AST transformation with the static initialization block {{
{Caller.mixin ExceptionHandler}}} in the Caller class.