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

JShell crashes when trying to throw a non-exist exception in certain lambdas

XMLWordPrintable

    • 16
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      When I try to throw a non-exist exception in a lambda declared in a default or static method in the corresponding functional interface, whose functional method is declared `throws Throwable`, it crashes.

      It also crashes when the interface has a type parameter `X extends Throwable` and the functional method is declared `throws X`.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Input the following code and do not define the `NotExist` class.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should output an error message that the symbol cannot be found.
      ACTUAL -
      It crashes with the following message:

      Exception in thread "main" java.lang.InternalError: Exception during analyze - java.lang.AssertionError: isSubtype UNKNOWN
      at jdk.jshell/jdk.jshell.TaskFactory$AnalyzeTask.analyze(TaskFactory.java:415)
      at jdk.jshell/jdk.jshell.TaskFactory$AnalyzeTask.<init>(TaskFactory.java:406)
      at jdk.jshell/jdk.jshell.TaskFactory.lambda$analyze$1(TaskFactory.java:178)
      at jdk.jshell/jdk.jshell.TaskFactory.lambda$runTask$4(TaskFactory.java:213)
      at jdk.compiler/com.sun.tools.javac.api.JavacTaskPool.getTask(JavacTaskPool.java:193)
      at jdk.jshell/jdk.jshell.TaskFactory.runTask(TaskFactory.java:206)
      at jdk.jshell/jdk.jshell.TaskFactory.analyze(TaskFactory.java:175)
      at jdk.jshell/jdk.jshell.TaskFactory.analyze(TaskFactory.java:161)
      at jdk.jshell/jdk.jshell.Eval.lambda$compileAndLoad$23(Eval.java:1066)
      at jdk.jshell/jdk.jshell.TaskFactory.lambda$runTask$4(TaskFactory.java:213)
      at jdk.compiler/com.sun.tools.javac.api.JavacTaskPool.getTask(JavacTaskPool.java:193)
      at jdk.jshell/jdk.jshell.TaskFactory.runTask(TaskFactory.java:206)
      at jdk.jshell/jdk.jshell.TaskFactory.analyze(TaskFactory.java:175)
      at jdk.jshell/jdk.jshell.TaskFactory.analyze(TaskFactory.java:161)
      at jdk.jshell/jdk.jshell.Eval.compileAndLoad(Eval.java:1060)
      at jdk.jshell/jdk.jshell.Eval.declare(Eval.java:893)
      at jdk.jshell/jdk.jshell.Eval.eval(Eval.java:140)
      at jdk.jshell/jdk.jshell.JShell.eval(JShell.java:493)
      at jdk.jshell/jdk.internal.jshell.tool.JShellTool.processSource(JShellTool.java:3624)
      at jdk.jshell/jdk.internal.jshell.tool.JShellTool.processSourceCatchingReset(JShellTool.java:1348)
      at jdk.jshell/jdk.internal.jshell.tool.JShellTool.processInput(JShellTool.java:1246)
      at jdk.jshell/jdk.internal.jshell.tool.JShellTool.run(JShellTool.java:1217)
      at jdk.jshell/jdk.internal.jshell.tool.JShellTool.start(JShellTool.java:1001)
      at jdk.jshell/jdk.internal.jshell.tool.JShellToolBuilder.start(JShellToolBuilder.java:261)
      at jdk.jshell/jdk.internal.jshell.tool.JShellToolProvider.main(JShellToolProvider.java:120)
      Caused by: java.lang.IllegalStateException: java.lang.AssertionError: isSubtype UNKNOWN
      at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.analyze(JavacTaskImpl.java:383)
      at jdk.jshell/jdk.jshell.TaskFactory$AnalyzeTask.analyze(TaskFactory.java:412)
      ... 24 more
      Caused by: java.lang.AssertionError: isSubtype UNKNOWN
      at jdk.compiler/com.sun.tools.javac.code.Types$4.visitType(Types.java:1133)
      at jdk.compiler/com.sun.tools.javac.code.Types$4.visitType(Types.java:1110)
      at jdk.compiler/com.sun.tools.javac.code.Type.accept(Type.java:228)
      at jdk.compiler/com.sun.tools.javac.code.Types$DefaultTypeVisitor.visit(Types.java:4906)
      at jdk.compiler/com.sun.tools.javac.code.Types.isSubtype(Types.java:1106)
      at jdk.compiler/com.sun.tools.javac.code.Types.isSubtype(Types.java:1077)
      at jdk.compiler/com.sun.tools.javac.comp.Check.subset(Check.java:1583)
      at jdk.compiler/com.sun.tools.javac.comp.Check.isHandled(Check.java:1681)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.markThrown(Flow.java:1164)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitThrow(Flow.java:1568)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCThrow.accept(JCTree.java:1736)
      at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:447)
      at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:57)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitBlock(Flow.java:1325)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1082)
      at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:447)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitMethodDef(Flow.java:1291)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:912)
      at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:447)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitClassDef(Flow.java:1254)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:810)
      at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:447)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitClassDef(Flow.java:1199)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:810)
      at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:447)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.analyzeTree(Flow.java:1669)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.analyzeTree(Flow.java:1659)
      at jdk.compiler/com.sun.tools.javac.comp.Flow.analyzeTree(Flow.java:225)
      at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1377)
      at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1341)
      at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.analyze(JavacTaskImpl.java:404)
      at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$analyze$1(JavacTaskImpl.java:379)
      at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.invocationHelper(JavacTaskImpl.java:152)
      at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.analyze(JavacTaskImpl.java:379)
      ... 25 more


      ---------- BEGIN SOURCE ----------
      // Case A
      @FunctionalInterface
      interface RunnableWithThrowable {
          void run() throws Throwable;

          // You can also replace `static` with `default` and test again
          static RunnableWithThrowable getInstance() {
              return () -> { throw new NotExist(); };
          }
      }

      // Case B
      @FunctionalInterface
      interface RunnableWithGenericException<X extends Throwable> {
          void run() throws X;

          static RunnableWithGenericException<?> getInstance() {
              return () -> { throw new NotExist(); };
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Declare a specific exception type (e.g. `throws Exception`) in the `throws` clause of the functional method.

      FREQUENCY : always


        1. RunnableWithGenericException.java
          0.2 kB
          Anupam Dev
        2. RunnableWithThrowable.java
          0.2 kB
          Anupam Dev
        3. Screenshot 2023-03-13 at 2.00.00 PM.png
          91 kB
          Weibing Xiao

            wxiao Weibing Xiao
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: