Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8187744

Labeled break in a function with throw statement causes incorrect return value

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.8.0_144"
      Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      macOS Sierra 10.12.6

      A DESCRIPTION OF THE PROBLEM :
      Sample script:

      var return5 = (function(x) {
        mark0: {
          var y;
          if (x !== null) {
            y = 5;
            break mark0
          };
          throw 'Bang!'
        }
        return y
      })
      print(return5(10))

      This prints 'undefined' in Nashorn instead of '5' as expected. If you comment out the throw 'Bang!' and run it prints '5', even though the throw is not hit. If you call the unedited function in Firefox or Chrome, you get '5' as expected.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Put a labelled break in a function that returns a value. Add a throw statement somewhere.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      We get the proper returned value, not 'undefined'.
      ACTUAL -
      We get 'undefined' as the returned value.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      var return5 = (function(x) {
        mark0: {
          var y;
          if (x !== null) {
            y = 5;
            break mark0
          };
          throw 'Bang!' // commenting out this line causes the result to be correct, even though it is not executed.
        }
        return y
      })
      print(return5(10)) // prints 'undefined' in Nashorn, not '5'.
      ---------- END SOURCE ----------

        1. testJI9050893.js
          0.3 kB
        2. testJI9050893.js
          0.3 kB
        3. JI9050893.java
          0.5 kB
        4. JI9050893.java
          0.5 kB

            pmuthuswamy Priya Lakshmi Muthuswamy (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: