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

record class declarations will not allow throws clause

    XMLWordPrintable

Details

    • Bug
    • Resolution: Not an Issue
    • P4
    • None
    • 17, 18
    • tools
    • generic
    • generic

    Description

      ADDITIONAL SYSTEM INFORMATION :
      Found and verified in Java 18

      A DESCRIPTION OF THE PROBLEM :
      I believe this is an error in the language definition. The record definition allows compact constructors, which may contain code that throws an exception (which may be a checked exception). But there is nowhere to place the throws clause for that exception.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a record class. Create a compact constructor for that class. Add a throw statement for a checked exception. The compiler will tell you that you need a throws clause. If you add a throws clause the compiler will tell you that you are missing an open brace or that the throws clause is not permitted

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected a clean compilation.
      ACTUAL -
      Code could not be compiled.

      ---------- BEGIN SOURCE ----------
      public class NewMain
      {
      public static void main(String[] args)
      {
      // TODO code application logic here
      }
      record Abc(int xyz)
      {
      Abc
      {
      throw new Exception)();
      }
      }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Activity

          People

            adev ANUPAM DEV
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: