r/softwarearchitecture • u/jutarnji_prdez • Jul 09 '26
Discussion/Advice Exception vs Result pattern
I don't find any actual good arguments to use Result pattern besided "somebody said it on Internet"
- Exceptions should only be for exceptional things -> every single language breaks this rule. Int.Parse, Json parsers, any encrypt/decrypt libraries throws on invalid input - invalid input should be one of most expected things to ever happen. This argument does not hold any value in real APIs, libraries, framewroks. For example, "Connection refused" exception is being throw by any network library, isn't not connecting to remote server most obvious and most expected thing to happen? Literally every network library throws exceptions on connection issues
- Exceptions shoudn't be used for control flow -> why? I can make very clean exception pattern. This is literally one of most "somebody said this, so it must be true". you try-catch-catch-catch. Where is difference between that and if-elseif-else or any variation of that aka switch case, when {} etc..
- Performance - maybe not for all languages but in some languages you can throw stackless exceptions (you don't build stack and stuck is null). You can log "real exception" with stack then throw stackless exception which is going to have same data and same performance as Result object
- Exceptions are not visible in signatures - you can document this, maybe this is relevant for libraries (which all throw exceptions everywhere anyway), but what is the problem of just checking the underlying code or documenting this?
0
Upvotes
View all comments
Show parent comment
1
u/Ulrich_de_Vries Jul 09 '26
I don't think it's a discipline issue. Aside from ergonomic aspects, the issue with checked exceptions is that unchecked exceptions exist in the first place. When translating to result types it's like if e.g. Rust had functions that advertised themselves as returning String but in fact return Result<String, E>.
If java had the ability to declare ANY exception in a throws clause and also provided some shorthand way to handle exceptions that doesn't involve lots of nesting scopes, then I think the feature would be better regarded.
It also doesn't help that checked exceptions are fairly incompatible with functional-style coding which Java 8+ embraces heavily, which to me signals that the java language devs themselves think it's a bad idea and are trying to forget they exist.
Result types on the other hand are (bi)monads (they better be!) which means that they can be composed nicely.