Language feature to improve checked exceptions

forax at univ-mlv.fr forax at univ-mlv.fr
Sun Mar 5 20:16:26 UTC 2023


> From: "Ron Pressler" <ron.pressler at oracle.com>
> To: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "Brian Goetz" <brian.goetz at oracle.com>, "Tom" <tom_l64 at hotmail.com>,
> "amber-dev" <amber-dev at openjdk.org>
> Sent: Sunday, March 5, 2023 8:37:41 PM
> Subject: Re: Language feature to improve checked exceptions

> Checked exceptions don’t intrinsically "colour your function” any more (or less)
> than a return type. Java’s ability to helpfully polymorphise over checked
> exceptions, however, is more limited.
That's factually correct but it's like saying that C# Task or JS Promise don't intrinsically color your function because they are return types. 

Unchecked exceptions let you insert functions that are unaware of the exception in between the function that may raise an exception and the function that recover from that exception. 
So checked exceptions compared to unchecked exceptions color your function. 

Rémi 

> — Ron

>> On 5 Mar 2023, at 18:37, Remi Forax < [ mailto:forax at univ-mlv.fr |
>> forax at univ-mlv.fr ] > wrote:

>>> From: "Brian Goetz" < [ mailto:brian.goetz at oracle.com | brian.goetz at oracle.com ]
>>> >
>>> To: "Tom L" < [ mailto:tom_l64 at hotmail.com | tom_l64 at hotmail.com ] >,
>>> "amber-dev" < [ mailto:amber-dev at openjdk.org | amber-dev at openjdk.org ] >
>>> Sent: Sunday, March 5, 2023 3:24:38 AM
>>> Subject: Re: Language feature to improve checked exceptions

>>> I have some sympathy for the desire to optimize catch-and-wrap; doing this the
>>> regular way is indeed syntactically painful when the actual business logic is
>>> small (which it often is.) Catch-and-wrap works well as an abstraction idiom
>>> when turning a low-level exception (e.g., IOException) into a higher-level one
>>> (e.g., XMLException); as higher-level library-code delegates to lower-level
>>> library code, it will want to remap low-level errors to higher-level ones.
>>> (Particularly interesting is catch-and-wrap at the method declaration level,
>>> with some sort of `throws X as Y`, as it represents the catch-and-wrap rules
>>> declaratively rather than imperatively.)

>>> However, the examples you give of catch-and-wrap are not really catch-and-wrap;
>>> they are catch-and-pretend-they-didn't-happen (by turning checked exceptions
>>> into unchecked ones.) While this would surely be popular among the "checked
>>> exceptions suck" crowd, this is not making error handling more reliable, it is
>>> just making it easier to ignore errors.
>> A remark, a catch-and-pretend-they-didn't-happen is just a catch-and-wrap
>> without the enclosing context.

>> For example, you can see
>> List<String> foo() {
>> ...
>> List<String> list = stream.map(it -> {
>> try {
>> return IO.something(it);
>> } catch(IOException e) {
>> throw new UncheckedIOException(e);
>> }
>> }).toList(),
>> return list;
>> }

>> but foo() is called like this
>> List<String> list;
>> try {
>> list = foo()
>> } catch(UncheckedIOException e) {
>> throw e.getCause();
>> }

>>> The Result approach is both safe and honest (and can be implemented today
>>> without language help), but is more foreign to Java developers. It operates by
>>> moving the side-channel result into the main channel, which enables monadic
>>> composition (turning a partial operation into a total one returning a
>>> Success|Fail union). With pattern matching in the language, this gets even more
>>> attractive. Still, I suspect that most of the "checked exceptions suck" crowd
>>> wouldn't thank us for kicking off the massive, decade-long migration from
>>> checked exceptions to the Either monad.
>> From Java the language POV, returning Either or using a checked exception both
>> side of the same coin, there are semantically equivalent.

>> As a proud member of the "checked exception suck in Java" crowd. The "in Java"
>> is important. Checked exceptions do not suck in the vacuum, it suck in Java
>> because on a higher level is goes against the freedom of composition and
>> because on a lower level the Java type system does not handle them well, no
>> union type (only precise rethrow), no proper way to bundle exceptions into a
>> type parameter.

>> It's the colored function problem [1]. Checked exceptions aka the Result monad
>> does not suck in Rust. Because inherently Rust is a language that consider
>> library composition less important than precise lifetime tracking or
>> asynchronous calls tracking, the whole language relies on colored functions.
>> But Java is not Rust, freedom of composition, freedom to use any libraries
>> whenever it was written, is an important part of the language. That's why Java
>> has a GC, lightweight threads, lambdas, erased generics or the concept of
>> binary backward compatibility. All those features enable free easy composition.

>> Sadly checked exception in Java colors functions, they create an unnecessary
>> artificial barrier to library compositions. That's why they suck in Java.

>> regards,
>> Rémi

>> [1] [ https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/
>> | https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ ]

>>> On 3/4/2023 6:49 PM, Tom L wrote:

>>>> Hello, it's the first time I send an email here, I have no idea how things work,
>>>> so I hope I am doing things somewhat correctly.

>>>> I wanted to make a suggestion about exceptions :
>>>> Checked exceptions are used as a meant of a second return type, which shouldn't
>>>> happen in most cases, and then, depending of what the caller wants to do, it
>>>> will handle it in a certain way. So in some sort, a checked exception is like
>>>> part of the return type, like you would have a Result<Success, Failure>,
>>>> compared to unchecked exception in java which are just supposed to be bugs.
>>>> But this feature causes some burden, so much so that some languages, even
>>>> languages compiling to java (ie kotlin) got rid of checked exceptions, and
>>>> other languages like Rust use a Result<Success, Failure> which, which, with
>>>> enough language constructs, can be quite good.
>>>> While I agree that not having checked exceptions nor Result but instead only
>>>> unchecked ones would be a bad idea, because it would mean that a part of the
>>>> return type is unknown, which we wouldn't want in a strongly typed language, I
>>>> believe some action needs to be taken.

>>>> In my opinion, one of its burdens is caused by the try-catch language feature :
>>>> String text;
>>>> try {
>>>> text = Files.readString(somePath);
>>>> } catch(IOException ex) {
>>>> throw new UncheckedIOException(ex);
>>>> }
>>>> //do something with text
>>>> This is a common example, of how you would use it : you want to read a string,
>>>> and an IO error shouldn't happen, so you fail-fast
>>>> The reason why I didn't use text inside the catch, is because the catch should
>>>> only be for this specific exception, and also it would add another level of
>>>> nesting, which would start to hurt when other ifs or try-catches appear.
>>>> This code is boilerplate and is error-prone, since you always have to repeat the
>>>> same lines, it is also weird for any non java programmer.
>>>> And this is far from being the worst, because another common example is with
>>>> streams, since you can't throw checked exception, you have to handle each time,
>>>> in each stream operation, and even if you wrap this code in methods and use
>>>> method references, it's still far less readable than having a short lambda
>>>> where you see exactly what the stream is doing.
>>>> If checked exceptions were instead a Result type, a solution to this problem
>>>> would be to make a unwrap() method (like in Rust, or like with Java's
>>>> Optional#orElseThrow())
>>>> String text = Files.readString(somePath).unwrap();

>>>> So my suggestion is that, since catching is a language feature, it can only be
>>>> dealt with another language feature :
>>>> String text = Files.readString(somePath) throw IOException ex as new
>>>> UncheckedIOException(ex);
>>>> If this method throws an IOException, it will rethrow it as an
>>>> UncheckedIOException.
>>>> About the syntax, I used "throw" since it's an already used keyword, but
>>>> depending of the meaning, it could be "catch" or whatever, and "as" could be
>>>> "->" if using a contextual keyword is too much.
>>>> This code could even be simplied as the following with new methods or language
>>>> features, in the future :
>>>> String text = Files.readString(somePath) throw IOException ex as ex.unchecked();
>>>> String text = Files.readString(somePath) throw IOException as unchecked;
>>>> etc.
>>>> Unchecked part could either be the same exception except the compiler ignores it
>>>> (I know this is possible since it's possible to throw a checked exception as an
>>>> unchecked), the idea is that since you are telling the compiler that if an
>>>> exception happens, then it should fail-fast, then the compiler should be able
>>>> to say "ok, I trust you".
>>>> An alternative would be that the unchecked simply wrap it in a RuntimeException
>>>> or a specific unchecked exception.

>>>> So, what's the point of this, does it only serve this use case ?
>>>> So first, is it important to note that it isn't just for checked -> fail-fast,
>>>> but also checked -> some other checked, when exception conversion is needed,
>>>> which can be useful, and also unchecked -> checked/unchecked, for example if an
>>>> API only provides an unchecked like Integer#parseInt.
>>>> Now about the use case : this syntax, isn't just a compressed try-catch, it's
>>>> also an expression, which is very important, because not only you can very
>>>> clearly handle this kind of cases, but it can provide easy to read, concise
>>>> code in lambdas and assignments, for example :
>>>> try (var files = Files.list(path) {
>>>> return files.filter(this::matcher)
>>>> .map(p -> Files.readString(p) throw IOException as unchecked)//Possible syntax
>>>> Files::readString throw IOException as unchecked ?
>>>> .toList();
>>>> }
>>>> And in the future, a new method could be added for streams called
>>>> .continueOnFailure(IOExcepion.class) (or UncheckedIOException if the unchecked
>>>> wraps the exception instead of marking it as unchecked) for example, which
>>>> would continue even if there is an exception.
>>>> An additional syntax could also be provided for cases where catching is used as
>>>> an if else :
>>>> OptionalInt parseInt(String s) {
>>>> return OptionalInt.of(Integer.parseInt(s)) catch NumberFormatException ->
>>>> OptionalInt.empty(); // Using -> syntax here instead of "as" to show that it
>>>> can also make sense
>>>> }

>>>> I hope it can fix this unholy war of checked exceptions which never seem to
>>>> advance.

>>>> Sincerely.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230305/65ff743d/attachment-0001.htm>


More information about the amber-dev mailing list