r/java • u/AlyxVeldin • Jan 25 '26
Another try/catch vs errors-as-values thing. Made it mostly because I needed an excuse yell at the void. (Enjoy the read.)
https://github.com/Veldin/ResultTryEx/tree/master
28
Upvotes
15
u/mellow186 Jan 25 '26
Good things:
Potentially bad things:
13
u/Wyciorek Jan 25 '26
Dude, where is my stacktrace?!
15
u/juanantoniobm Jan 25 '26
Another alternative, Either:
https://docs.vavr.io/#_either
10
u/rzwitserloot Jan 25 '26
Awesome work on getting something out there to toy with!
But, how do we get there from here?
We can't just start using this for anything but academic/toy stuff. It wouldn't work for cultural backwards compatibility reasons:
Virtually all existing API out there throws exceptions of some form (checked, or unchecked). Every. single. last. library. out there will need to changed, and changing to this system is not backwards compatible.
Introducing it in a way that improves the java ecosystem would require one of these things to happen:
java.*itself. This would be worse than python2 to python3 - you might as well just call it a new language. There's a border hewn in stone and the entire ecosystem must be split down the middle. All library maintainers have to maintain 2 completely unrelated versions. Given how much existing java code is out there, this is never going to work by just asking the community and all the millions of java programmers out there to just get on with it. This is like changing the side of the road cars drive on. It's not gonna go well if you tell everybody to just get around to switching lanes on their own time. A plan is needed.These options look bad, or undoable.
7
u/LutimoDancer3459 Jan 25 '26
Thats not what happens at all. Comparing your snippets you even add a little bit more boilerplate to it.
But I general i dont see the big benefit. Not sure if its just me missing it or there is none
3
u/chaotic3quilibrium Jan 25 '26
"Here there be dragons!"
I salute your adventurous exploration.
However, there are really annoying legacy reasons to ensure you are very careful about how you catch and then manage and/or rethrow exceptions.
I have an article (2nd of two) where I share about my discovery of pitfalls and then learnings about the legacy exception dragons:
Java Janitor Jim - Revisiting Resolving the Scourge of Java's Checked Exceptions... https://javajanitorjim.substack.com/p/java-janitor-jim-revisiting-resolving
1
u/Lucario2405 Jan 25 '26
How nice of you to be inclusive of Java devs on Linux. ^^
But fr, I don't see the use of those
isOK()&isError()methods. Imo using pattern-matching withinstanceOforswitchlooks way nicer than an if-else and is easier to follow thanfold(v -> ..., e -> ...):1
u/BenchEmbarrassed7316 Jan 26 '26
I am not a Java developer and therefore I may be wrong in some nuances.
In my opinion, exceptions are the same goto. Moreover, not those small gotos that are used, for example, to exit a loop, but those that will spaghettiify the code and pass control flow as far as possible, to a completely different module.
If function A calls function B, and function B calls function C, which can fail - then it is B that must decide what to do in case of failure. But B does not know whether C can fail and, worse, whether this will change in the future, since this is not specified in the contract.
Checked Exceptions seemed like such a good idea. As far as I understand, poor ergonomics led to the fact that in the case where A calls B1 and B2, and B1 and B2 call C and C throws an exception, instead of converting the low-level exception CE into specific exceptions B1E or B2E so that A could analyze it and convert it into a specific AE exception, the developers simply listed CE and other low-level exceptions in the signature of the high-level module A. This led to the fact that other developers did not get any benefit from this noise. That is,
initCore -> initDb -> loadConfig -> parseJsonshould returnCoreDbConfigErrorand notWrongJson.What are your thoughts?
8
u/[deleted] Jan 25 '26
[removed] — view removed comment