Checked Exceptions Debate
Cluster focuses on debates about the pros and cons of checked exceptions in Java, comparing them to unchecked exceptions, error codes, and alternatives like Rust's Result types.
Activity Over Time
Top Contributors
Keywords
Sample Comments
Let's revisit past conversations:- https://news.ycombinator.com/item?id=43226624- https://news.ycombinator.com/item?id=43584056- https://news.ycombinator.com/item?id=36736326And more. I'm not sure what you fo
Checked exceptions are hardly Java's biggest mistake.
Checked exceptions are more trouble than they're worth. That doesn't make exceptions in general bad.
Java has checked exceptions, but everyone seems to hate them.
Worse than checked exceptions (Java)? How about unchecked exceptions?
Java did a shitty job of checked exceptions. C++ did worse.
I like checked exceptions because you can't not deal with the error condition. Swallowed exceptions is a code smell that any half-decent developer will notice, whereas failing to check for some return value is much more subtle. Java allows you to have both, since you can design your exceptions as unchecked, or rethrow a checked exception wrapped in a runtime exception.
Agreed, checked exceptions surface potential errors to the caller. In that regard Java fares better. However, handling exceptions still require a separate catch clause rather than being part of the normal flow. Furthermore, it is nigh impossible to rely on checked exceptions alone, most Java code may throw RuntimeExceptions such as NullPointerException or ArrayIndexOutOfBoundsException and there is no way to know it by just looking at the calling code.
The difference between error codes and exceptions are that, for error codes, you must check the result of every function call and method and in some way handle that result. Contrast this to exceptions, where the advantage is that your error handling is centralized. You should throw as much as necessary and catch in a few key places.Checked exceptions ruins that advantage of exceptions -- you're back to writing a whole bunch of buggy error handling code rather than just a few key catches. T
So you think java's checked exceptions are a better model? No opinion myself, but that way seems widely considered bad too.