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

Compiler error for throw (ternary operation) expression

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 7u45
    • tools
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.7.0_45"
      Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 Enterprise x64

      A DESCRIPTION OF THE PROBLEM :
      Consider the following method:

        void foo(Throwable error) throws InterruptedException
        {
          throw (error instanceof InterruptedException ? (InterruptedException)error : new RuntimeException(error));
        }

      Java compiler reports the following error on the "throw" line:

      Error:(...) java: unreported exception java.lang.Exception; must be caught or declared to be thrown.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create class with the following method and try to compile it:

        void foo(Throwable error) throws InterruptedException
        {
          throw (error instanceof InterruptedException ? (InterruptedException)error : new RuntimeException(error));
        }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No compiler error.
      ACTUAL -
      Compiler error on the "throw" line:

      Error:(...) java: unreported exception java.lang.Exception; must be caught or declared to be thrown.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Error:(...) java: unreported exception java.lang.Exception; must be caught or declared to be thrown.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
        void foo(Throwable error) throws InterruptedException
        {
          throw (error instanceof InterruptedException ? (InterruptedException)error : new RuntimeException(error));
        }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      If code is refactored as the following, the compiler does not throw an error:

        void foo(Throwable error) throws InterruptedException
        {
           if(error instanceof InterruptedException)
             throw (InterruptedException)error;
         
           throw new RuntimeException(error);
        }

            jlahoda Jan Lahoda
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: