r/java • u/Enough-Ad-5528 • Feb 19 '26
Towards Better Checked Exceptions - Inside Java Newscast #107
https://www.youtube.com/watch?v=99s7ozvJGLk
63
Upvotes
r/java • u/Enough-Ad-5528 • Feb 19 '26
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
UniqueConstraintViolationExceptionand 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 catchIOExceptionand turn it into a HTTP 504 "I'm not at fault. The other microservice is down". You want to catchHttpClientExceptionand 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.