Throwing exceptions from records

Vicente Romero vicente.romero at oracle.com
Thu Mar 5 19:39:05 UTC 2020


Hi Ty,

On 3/5/20 2:12 PM, Ty Young wrote:
>
> Actually going to expand this a bit after messing around with records...
>
>
> How is records supposed to work beyond the most basic usage of 
> Point(int x, int y)? There isn't much information out there for the 
> more advanced usages so I'm confused.
>
>
> To go into more detail, I'm trying to convert this:
>
>
> https://github.com/BlueGoliath/Goliath-Nvidia-Bindings/blob/master/modules/org.goliath.bindings.nvml/src/main/java/org/goliath/bindings/nvml/functions/nvmlInit.java 
>
>
>
> which doesn't seem possible as-is because you can't throw exceptions 
> from record constructors.

you can't throw exceptions from _canonical_ record constructors, being a 
canonical record constructor the one with the same signature as the 
record header but you can do:

record R(int i) {
     public R() throws Exception {  // not a canonical constructor so no 
restriction
         this(someMethodReturningIntThatThrows());  // invoking the 
canonical constructor
     }
}
>
>
> Furthermore there seems to be some constraint where constructor 
> arguments have to match the record argument signature. Why does that 
> even matter?

only the canonical but you can declare other constructors with a 
different signature
>
>
> As a result, I can't convert classes like:
>
>
> https://github.com/BlueGoliath/Crosspoint/blob/master/src/main/java/org/goliath/crosspoint/unions/BasicNativeUnion.java 
>
>
>
> to a record.

HTH,

Vicente
>
>
> On 3/5/20 11:16 AM, Alex Buckley wrote:
>> Per JEP 359, the list for record-related discussion is amber-dev. 
>> bcc'ing jdk-dev off this thread.
>>
>> On 3/5/2020 9:12 AM, Ty Young wrote:
>>> Hi,
>>>
>>>
>>> Is there a way in JDK 14 records to throw exceptions from a 
>>> constructor? Currently I have dozens of classes that I want to 
>>> convert to records but am unable to due to there being no way to 
>>> have a constructor that throws exceptions. I'm not able to find any 
>>> examples of how records and exceptions are supposed to work together 
>>> online either...



More information about the amber-dev mailing list