3 ms·
You can but should you in some particular way? It's often said that checked exceptions are the worst mistake in Java and I believe that. To be specific consid
by PaulHoule 2y ago
You can but should you in some particular way?
It's often said that checked exceptions are the worst mistake in Java and I believe that.
To be specific consider the case of a class that implements FetchSomeData which fetches some data (could just as well be a function with parameters encoded inside it as a closure) which could come from many sources such as
* hard coded in the class
* read from a file
* fetched via http
* fetched from Postgresql or CouchDB or ...
the one thing these have in common is they can fail in very different ways. If you want to take encapsulation seriously here the right way to do it is to expose certain semantics of the error such as
* Could the user avoid this problem by changing their inputs?
* What do we tell the end user? What do we tell the sysadmin?
* Is it likely that this problem will clear up if we retry the request in five minutes?
* Is attempting to use this object likely to cause worsening data corruption
and such. In Java we get the very week beer of people creating a large number of exceptions like FailureinSubsystemSeventeenException and sprinking thousands of methods with throws clauses and doing a lot of error-prone catching and rethrowing to please the compiler but if you really are serious about error handling you know you could get a RuntimeException because something wrote a[15] on an array that has only 12 elements and you still need to catch those anyway.
So far systems that use something like Either[Result|Exception] aren't based on an ontology of failure but my experience is that code written like that tends to have the same problems you have with checked exceptions: harried by the compiler people often omit error handling code to the maximum extent that they can. From working with both kinds of code bases I'd say that code bases worked on exceptions have the glass half full and handle errors properly more than they do improperly, these monad-based systems are the other way around.
- LelouBil 2y agoI'd say if you have checked exceptions, you need great ergonomics to handle them, transform them and ignore them. Java has poor ergonomics for these, and in my opinion languages like Rust have better ones and that's why they manage with Results.
- vips7L 2y agoErrors/Exceptions are just way too contextual without some sort of operator to say “hey trust me I know this can’t happen”. I’d like to see Java adopt something like Kotlins !! operator rather than coloring the stack with throws clauses. Otherwise your code is just littered with try/catch/throw new RuntimeException(ex).
- PaulHoule 2y ago!! is poison but what is priceless in the JVM is attaching an exception handler to each thread so exceptions don’t fall off ignored, mostly you don’t want to catch exceptions if you can but instead use finally as much as possible to get the state right on both happy and sad paths, if you must rethrow use a rethrower function that handles InterruptedExeptions!
- vips7L 2y agoI’m not sure !! is poison. I see it the same as rust’s .unwrap() calls.