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

Multi-catch in a lambda expression catches any Exceptions

    XMLWordPrintable

    Details

    • Subcomponent:
    • Introduced In Version:
      8

      Description

      FULL PRODUCT VERSION :
      openjdk version " 1.8.0-ea "
      OpenJDK Runtime Environment (build 1.8.0-ea-lambda-nightly-h3876-20130403-b84-b00)
      OpenJDK Server VM (build 25.0-b21, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux flappie 3.5.0-26-generic #42-Ubuntu SMP Fri Mar 8 23:20:06 UTC 2013 i686 i686 i686 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      Hi,
        To start with, I am not sure if this is the correct place to report bugs for java 8, nor which package this bug is in, but I will leave the precise assignment and classification to whoever knows better.

      The problem is that when doing a multi-catch inside a lambda expression, it seems to be converted to a normal single-catch that catches the common superclass of the given exception types. This means that, for example, any kind of RuntimeException will be caught, instead of just the ones that are mentioned in the catch clause.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      public class TestMultiCatch {
      public static void main(String[] args) {
      Runnable lambda1 = () -> {
      try {
      throw new ClassCastException(); // Can also be RuntimeException or any other subclass of it
      } catch (EnumConstantNotPresentException | ArrayIndexOutOfBoundsException ex) { // Some arbitrary Exceptions
      System.out.println( " multi-catch: " + ex);
      }
      };
      lambda1.run(); // This (erroneously?) prints " multi-catch: java.lang.RuntimeException "

      Runnable lambda2 = () -> {
      try {
      throw new ClassCastException();
      } catch (EnumConstantNotPresentException ex) {
      System.out.println( " catch 1: " + ex);
      } catch (ArrayIndexOutOfBoundsException ex) {
      System.out.println( " catch 2: " + ex);
      }
      };
      lambda2.run(); // This throws a ClassCastException, like it should
      }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Exception in thread " main " java.lang.ClassCastException
      at TestMultiCatch.lambda$0(TestMultiCatch.java:5)
      at TestMultiCatch$$Lambda$1.run(Unknown Source)
      at TestMultiCatch.main(TestMultiCatch.java:10)
      Java Result: 1
      ACTUAL -
      multi-catch: java.lang.ClassCastException
      Exception in thread " main " java.lang.ClassCastException
      at TestMultiCatch.lambda$1(TestMultiCatch.java:14)
      at TestMultiCatch$$Lambda$2.run(Unknown Source)
      at TestMultiCatch.main(TestMultiCatch.java:21)
      Java Result: 1

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      See Steps to Reproduce.
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Can be worked around by not using multi-catch but stating each catch clause separately.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              rfield Robert Field
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: