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

ReturnAdder shouldn't add return if there's one in a finally block

Agile BoardAttach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.3.8, 2.4.0-beta-4
    • None
    • None

    Description

      The following code was written for a "Groovy puzzlers" talk:

       class CountDown { int counter = 10 }
      
      CountDown finalCountDown() {
          def countDown = new CountDown()
          try {
              countDown.counter = --countDown.counter
          } catch (ignored) {
              println "That will never happen. ${ignored.message}"
              countDown.counter = Integer.MIN_VALUE
          } finally {
              return countDown
          }
      }
      
      println finalCountDown().counter
      

      At execution, it goes to the catch because of a class cast exception:

      "Cannot cast object '9' with class 'java.lang.Integer' to class 'CountDown'"

      The reason is highlighted after returns have been added:

      java.lang.Object countDown = new CountDown()
              try {
                  return countDown .counter = --( countDown .counter)
              } 
              catch (java.lang.Exception ignored) {
                  this.println("That will never happen. $ignored.message")
                  return countDown .counter = java.lang.Integer.MIN_VALUE
              } 
              finally { 
                  return countDown 
              } 
      

      A "return" is added to the try branch, even though there's a return in the finally block which should always be executed. Adding a single statement after the finally block solves the issue because no return would then be added to the try block...

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            blackdrag Jochen Theodorou
            melix C├ędric Champeau
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Issue deployment