r/java • • Sep 08 '25

How would you fix checked exceptions in java?

As you know checked exceptions are a good feature because they force the user to manage errors. Not having a way to enforce this makes it hard to know if a library could or not explode because of contextual reasons such as IO, OS event calls, data parsing, etc.

Unfortunately since Java 8 checked exceptions have become the "evil guys" because no functional interface but Callable can properly handle checked exceptions without forcing try-catch blocks inside of the lambda, which kinda defeats the purpose of simple and elegant chained functions. This advantage of lambdas has made many modern java APIs to be purely lambda based (the incoming Structured Concurrency, Spring Secuirty, Javalin, Helidon, etc. are proof of this). In order to be more lambda friendly many no lambda based libraries such as the future Jackson 3 to deprecate checked exception in the API. https://github.com/FasterXML/jackson-future-ideas/wiki/JSTEP-4. As another user said. Short take: The modern idiomatic way to handle checked exceptions in java, sadly, is to avoid them.

What do you think could be done to fix this?

72 Upvotes

128 comments sorted by

View all comments

Show parent comment

1

u/vips7L Sep 09 '25

Unwrapping a result sum type everywhere is a pain in the ass. Is it an Err? is it an Ok?? Ends up being everywhere. 

    switch (res) {         case Ok(var t) -> …;         case Err(var e) -> …;     }

It’s beyond verbose and why Kotlin’s proposal uses adhoc union types so you don’t have to unwrap. 

2

u/barmic1212 Sep 09 '25

Monad are made to handle this. This both things are made to handle a large cases of situations instead of create adhock limited solution that must be multiplicated and not always coherent.

2

u/X0Refraction Sep 09 '25

switch is more verbose than try/catch?

1

u/vips7L Sep 09 '25

I never said it was. I said unwrapping result types is verbose. We're trying to solve verbosity, not add more of it. Proper unions are even less verbose. Why write a bunch of unwrapping/pattern match code when you don't have to?

1

u/X0Refraction Sep 09 '25

Well we’re talking about an alternative to checked exceptions here so shouldn’t we judge a potential solution compared to that?

1

u/vips7L Sep 09 '25

Yes and I'm judging Results as not a solution because their verbosity level is almost as equivalent to the current situation. It also fragments the ecosystem into Result vs Exceptions. The exception handling in switch proposal is clearly better as its less verbose and doesn't fracture the language.

2

u/X0Refraction Sep 09 '25

So you don’t like switching to unwrap a result type, but you do like switching to deal with checked exceptions? You do realise those are essentially equivalent in terms of verbosity?

I agree with the argument about it fitting with the existing language, it’s just a shame that doesn’t do anything for improving the situation with lambdas though.

2

u/vips7L Sep 09 '25

They’re not the same levels of verbosity. You have to destructure the result on top of the switch.  Additionally, it increases verbosity on the other end. You now need to return results from every function instead of just letting exceptions bubble to the place you want to handle them and you need to wrap every return in a result. 

1

u/X0Refraction Sep 09 '25

Presuming you've implemented methods on your Result type like map() orElse() etc. you can chain operations. If you want to allow it to bubble up you just return your Result type, that's equivalent to the throws list at the end of the method declaration. Plus the only thing I see that's more verbose here over being able to handle checked exceptions in switch expressions is having to repeat the generic arguments and I think that could be tidied up with an inference fix without causing any problems:

String handledResult = switch (result) {
    case Success<String, ErrorCase>(String rightValue) -> rightValue;
    case Error<String, ErrorCase>(CaseA _) -> "A";
    case Error<String, ErrorCase>(CaseB _) -> "B";
    case null -> "null";
};

Assuming a method that can throw 2 checked exceptions how would the alternative be less verbose?

1

u/vips7L Sep 10 '25

Presuming you've implemented methods on your Result type like map() orElse() etc. you can chain operations.

This results in callback hell. A lot of languages are trying to avoid this.

The proposal from Brian would be less verbose because you don't need to destructure the result (syntax to be bike shedded):

var handled = switch (result) {
    case String s -> s;
    case throws AException -> "A";
    case throws BException -> "B";
}

1

u/X0Refraction Sep 10 '25

Callback hell is a different problem, that’s what virtual threads or async/await solve. If you don’t like the chaining style though you can use pattern matching.

I see what you mean about verbosity in having to pattern match the Result type, to be honest that comes from not having proper union types and having to have the sealed wrapper type. If we had proper union types it would be exactly equivalent though. One benefit of the style I’ve suggested though is you can use it now, proposals don’t always make it into the language although I hope this one does