Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4801

Thrown exception not accessable inside catch block when class has @Category transformation applied

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.7.10
    • Fix Version/s: 1.8.1
    • Component/s: ast builder
    • Labels:
      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.

        Attachments

          Activity

            People

            • Assignee:
              paulk Paul King
              Reporter:
              bunglefeet Ewan Dawson
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: