Created
September 13, 2024 13:29
-
-
Save donalmurtagh/795984103e639a4aaf2e0fb6a6019c16 to your computer and use it in GitHub Desktop.
A more realistic version of the code sample on https://result.leakyabstractions.com/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int getServerUptime() throws ConnectionException { | |
var server = connect(); | |
server.refresh(); | |
return server.getUptime(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I disagree. The right thing to do is to handle the exception where it makes sense, and to propagate it where it makes sense. And that depends on the specific semantics of the method invoking the exception-throwing API. There is no silver bullet strategy.
For what is worth, I don't think there is a consensus. And my quarrel is not with checked exceptions, anyway.
The problem Results are trying to solve is twofold:
Checked exceptions meet requirement #1, but the way they are designed in Java makes it hard to use such APIs freely with
Function
,Predicate
,Supplier
,Consumer
, and the like. That's why checked exceptions are not a convenient solution for me.Unchecked exceptions, on the other hand, meet requirement #2. But they remain documented by means of Javadoc only. The compiler will not complain if I neglect the possibility of errors. And that's why unchecked exceptions are not a solution I can rely on, either.
As a beneficial side effect, programs tend to run faster once you remove the overhead of exceptions. All in all, I'm better off using Results than using exceptions.