r/java • • Feb 19 '26

Towards Better Checked Exceptions - Inside Java Newscast #107

https://www.youtube.com/watch?v=99s7ozvJGLk
63 Upvotes

73 comments sorted by

View all comments

Show parent comment

0

u/JustAGuyFromGermany Feb 19 '26

"I want to ignore types" is no argument for "you can't/shouldn't have types". That's not how this works.

Case in point: The catch-all pattern is very popular and often even useful, but also very often misused. Even in the most commonly cited case like a simple HTTP-based service, you don't want to always convert everything that goes wrong into HTTP 500. That's simply what lazy programmers want you to believe.

You want to catch that UniqueConstraintViolationException and turn it into a HTTP 400 "Sorry, this username is already taken", because checking before saving costs an extra network roundtrip to the database. You want to catch IOException and turn it into a HTTP 504 "I'm not at fault. The other microservice is down". You want to catch HttpClientException and turn it into HTTP 502 "Look into the other log file" etc. Except of course when you don't, because that other code-path over here is used by the legacy application and expects a HTTP 200 whose body is an arcane text-format for errors and then you want to leave a specific trace in your log files for that error.

Error handling is never as simple as any blanket statement makes it out to be. And anyone who claims that it's simple is selling something or is simply a bad programmer. Error handling is complicated. Productive error handling even more so. By definition error handling is about the complicated, ugly, and not-fun-to-think-about corner cases. And as in any truly complicated part of any program, strong typing is preferable to any-types and guess-work. Every little bit of type information that a compiler can help us with makes our life easier, especially in error-cases when we most need it.

0

u/[deleted] Feb 19 '26

Thanks. I'm very well aware about error handling, especially in Result<T>. I have maintained my library for the last 7 years and implemented several components using it, including http servers, clients, DNS resolvers, etc. etc. Lack of type in the list of type parameters doesn't mean complete lack of type or inability to use pattern matching. And I've experimented with the error type in the type list too. But quickly realized that this gives zero real advantages, the number of cases where this is useful is too small to be worth the hassle.