I started to use Java Records with the Binder and noticed that writeRecord is throwing a ValdidationException and this is a checked exception that I have to handle.
I didn’t notice that so far because with classes I use writeBeanIfValid and this returns a boolean.
In case of writeBean method it is sort of obvious, as it is “public void writeBean(BEAN)” by signature. Technically writeRecord could have alternative implementation of just returning null when validations not passing, or use Optional wrapping.
Yes, because there is no return value. Otherwise you can’t know if the validations passed, so with this method it is mandatory. If you do not like to implement this via catching exception, writeBeanIfValid can be used instead.
We should probably add a similar writeRecordIfValid method as well.
But I disagree that checked exceptions are a universal mistake. As an API designer, there are cases where it makes sense to “force” the user to take the possibility of an “expected” and recoverable error case into account. The other way for doing that for a method that also needs to return a value would be to return some kind of ResultOrError object and have the developer do conditional checking on the value type. This used to be even more inconvenient than checked exceptions before Java got sealed types and type patterns.