r/java • u/AlyxVeldin • Jan 28 '26
Throwing is fun, catching not so much. That’s the real problem IMO.
Two days ago I made a 'Another try/catch vs errors-as-values thing.' Thanks for all the comments and discussion guys.
I realised though I might not have framed my problem quite as well as I hoped. So I updated a part of my readme rant, that I would love to lay here on your feets aswell.
Throwing is fun,
catching not so much
For every exception thrown, there are two parties involved: the Thrower and the Catcher. The one who makes the mess, and the one who has to clean it up.
In this repo, you won’t find any examples where throw statements are replaced with some ResultEx return type. This is because I think there is no way we can just do away with Throw, not without fundamentally changing the language to such a degree that it is a new language. But most importantly, I don't think we should do away with Throwing at all.
The problem isn’t throwing, Throwing exceptions is fun as f*ck. The problem is catching. Catching kinda sucks sometimes right now.
What I want to see is a Java future where the catching party has real choice. Where we can still catch the “traditional” way, with fast supported wel established try-catch statements. But we’re also free to opt into inferrable types that treat exceptions-as-state. Exception-as-values. Exception-as-data. Whatever you want to call it.
And hey, when we can't handle an exception it in our shit code, we just throw the exception up again. And then it's the next guy's problem. Let the client side choose how they want to catch.
So keep throwing as first-party, but have the client party chose between try-catch and exception-as-values.
This way, no old libs need to change, no old code needs to change, but in our domain, in our code, we get to decide how exceptions are handled. Kumbaya, My Lord.
And yes: to really make this work, you’d need full language support.
Warnings when results are ignored. Exhaustiveness checks. Preserved stack traces.
Tooling that forces you to look at failure paths instead of politely pretending they don’t exist.
39
u/AnyPhotograph7804 Jan 28 '26
Error handling is never fun because you have to live with compromises when you do it. Checked exceptions can be annoying, errors as return types can be ignored and the application simply crashes etc.
7
u/mellow186 Jan 28 '26
What advantage do you see to treating exceptions as return values?
Because there are some serious disadvantages.
7
u/LutimoDancer3459 Jan 28 '26
Exceptions are and should always be exactly that. And Exception. If you just add tons of exceptions everywhere to also handle the state and flow of the application, you have a design problem.
And if I check for an error return value or an exception is the same in the end. While try catch has benefits like autocloseable.
7
u/pron98 Jan 28 '26
But Java's exceptions are already values. The problem is that the type system doesn't generify over exception types well. In fact, if you want to use syntax like
Either<String, IOException> foo()rather thanString foo() throws IOException- which is the same thing but with the Either type being built into the language - you'll run into the same issues around multiple exception types (i.e. union types). But these issues can be solved even for the built-in syntax. It's not a different concept, just different syntax. Exception types would flow through lambdas and stream pipelines; using an explicit Either type doesn't make anything any easier.6
u/EagleSwiony Jan 28 '26
I would rather just blindly catch any exception (checked or unchecked) with catch(Excpetion e) than to introduce Result-type exceptions.
2
u/CompetitiveSubset Jan 28 '26 edited Jan 30 '26
As a server side dev, Runtime exceptions are horrible, but they are the best compromise for ”off happy path” stuff I seen in any lang. fight me.
2
u/rzwitserloot Jan 29 '26
In most languages, there's an ocean of difference between 'expectable' errors and 'less expectable' errors.
For example, in go's error handling, the method's return type in essence 'specs' its expectable errors. This mechanism is not used to convey, for example, out of memory issues, app load errors (
ClassNotFoundErrorand their equivalents in other languages), and all sorts of other errors that certainly can and do happen but which aren't expectable.In scala, the concept 'checked exception' does not exist, instead they're all unchecked. If my earlier description of 'religious zealouts that shit down your throat if you dare to question the faith' applies to any community, it surely applies to them, and indeed this bizarre untyped faux pas is a bizarre omission in this type-fanaticism. Except, scala 'did the thing' and introduced either types to deal with it. Thus, here too: The way something like a 'memory error' is conveyed vs how e.g. 'file not found' on
file.openis handled is guns and grandmas: Completely different.Java is in the minority where that's not really the case. Both types of errors are handled in the same way: With exceptions. You catch them.
Java still does differentiate between this concept of 'expectable' and 'less expectable' via unchecked v checked. The less expectables are unchecked, the rest is checked.
In reality, the world just aint that nice. This isn't a black and white thing, it's shades of gray. There is no clear and unambigious dividing line between these two.
Which therefore proves that all these languages except java suck in this regard. It's objectively incorrect to split up error handling like this; this peddles the notion that the difference between these categories is far more clearly delineated than it really is. At best, it was the lesser evil, and that may be the case.
If you're not convinced - I can understand that, the comment is long enough, we can hold a separate debate about this shades of gray issue.
But given that this is how it works, java is better than either types.
Except, indeed, try/catch is a bit of a mouthful. It's great in situations where you want to guard a sizable block of code for a certain error condition that can occur anywhere inside it. It's far less good when you want to 'pinpoint deal with it' - when you want to "call this method; if the file can be opened, do X, if it cannot be opened, do Y". Then try/catch is a lot of code.
Fixing that is possible... I think. Next comment for more.
1
u/javaprof Jan 28 '26
Exactly, see my adoptation of Go/Zig/Kotlin ideas but for Java: https://www.reddit.com/r/java/comments/1n1blgx/community_jep_explicit_results_recoverable_errors/
1
u/DGolden Jan 29 '26 edited Jan 29 '26
Well, there are other things in the language design space than "Exceptions" or "Errors as values" like Common Lisp (and a few other languages, if mostly other Lisps/Schemes) with Condition Systems instead.
https://gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html
Dylan is Lisp-influenced but doesn't use Lisp/Scheme syntax.
https://opendylan.org/books/drm/Signalers_Conditions_and_Handlers
1
u/sviperll Jan 29 '26
Have you seen https://github.com/sviperll/result4j ?
There you can have "catchers":
1
u/rzwitserloot Jan 30 '26
Thinking about it some more, I think I have it. This is:
Here it is:
try-as-expression
try-else
This means: Execute 'exprThatThrows()'. If it succeeds, the whole expression's value is that. If it throws any exception thrown by a method that its 'throws' block declares, then the 'else' is returned. If it throws anything else, that just happens (because falling back to the 'else' when e.g. an
OutOfMemoryErroroccurs is not what folks would/should expect).In other words, this:
Does what you think, if
parseIntis explicitly declared asthrows NumberFormatException(which it should be).Downside: That now means adding unchecked exceptions to your
throwsline has an effect, and the compiler can't operate by 'virtually' treating all method signatures as if they all havethrows RuntimeException, Error.The actual expression (
exprThatThrows();in this code) can also be a block ({}), in which case the block needs ayieldstatement. Same goes for the 'else' which can also be a block, in which case it 'returns' a value via 'yield'.try-catch-expression
0
u/endeavourl Jan 28 '26
Not really? Like, i'm not a child?
-2
u/TallGreenhouseGuy Jan 28 '26
Anders Hejlsberg (lead C# architect) had this to say back in the days why they didn’t add checked exceptions to C# because of how it was used in Java:
: The scalability issue is somewhat related to the versionability issue. In the small, checked exceptions are very enticing. With a little example, you can show that you've actually checked that you caught the FileNotFoundException, and isn't that great? Well, that's fine when you're just calling one API. The trouble begins when you start building big systems where you're talking to four or five different subsystems. Each subsystem throws four to ten exceptions. Now, each time you walk up the ladder of aggregation, you have this exponential hierarchy below you of exceptions you have to deal with. You end up having to declare 40 exceptions that you might throw. And once you aggregate that with another subsystem you've got 80 exceptions in your throws clause. It just balloons out of control.
26
u/[deleted] Jan 28 '26
[removed] — view removed comment