It's good you don't actually need to know that! And if you did, it wouldn't matter anyway because somebody could come along in the future and change that code that currently uses a database and change it a web service and all that knowledge would be wasted.
All you need to know is what exceptions you can handle and where you handle them. What error codes or exceptions each individual function everywhere up the call chain can raise is almost inconsequential.
Ah, so that's "all" I need to know!
And that's what typed errors do.
Do I need to check the return type of the thousands of methods that might make up that operation? Nope. Typed errors make me do a lot of work passing stuff around and declaring types that literally don't matter. If somebody adds a new method that does another network call, I'm still good and I didn't have to do anything.
I don't think we can discuss deeper than that.
The other thing I don't like is pointless boilerplate code.
But I do like strongly-typed languages and originally I thought I'd like checked exceptions before realizing just how wrong that concept is. It's not hard to see the parallels between checked exceptions and error returns.
My choice of language wouldn't have the concept of "null".
If it somehow returns an Option type... well, the docs would say it, and it won't typecheck unless I handle it.
> If you want strongly typed errors then you could use a language with checked exceptions (Java).
Yes, typed error handling looks an awful lot like Java's checked exceptions.
Buuuuut exceptions magically unwind the stack at unknown points. I don't like my stacks unwinded unless I explicitly tell them to. Function exit points should be explicit.
Also: it has unchecked exceptions too, runtime exceptions (null pointers, anyone?) and lacks the expressiveness to handle error propagation while not being a pain in the ass.
Disagree, in my favorite language you just add "?" to the possibly-erroring function et voilá, error propagated. Even better, since errors are functor-like (map) and monad-like (and_then), I'm mostly never unwrapping myself.
This has the advantage that it's explicit, instead of exceptions implicitly returning early (sometimes to great amusement).
And one of the first things I had to learn in rust was how make this happen with different error types. After that it was pretty much the same as exceptions. What functions return errors is a little more obvious, at the cost of extra boiler plate (most stuff being Result<...> anyway)
Checked exceptions Java style could be a perfect balance with just a few modifications. Compiler/IDE should mostly know whats thrown where, so specifying it manually should rarely be necessary. And still could visualize what can fail and with what errors.
But is this different from error codes? Does seeing a file open tell you every error that might occur from that?
If you're arguing that you could just look at the file-open code to see the errors it returns, couldn't the same apply for seeing which exceptions are thrown?
No, because then you have to look at every function inside there. And there every function inside those functions. And then...
With typed error handling they're only one-level of abstraction deep. Actually zero-levels, since your function won't typecheck unless you handle the errors.
Mostly yes, usually as a sum type.
Pattern matching is your friend.
> the errors are massaged
Why would I take the extra effort?
Unless it's my responsibility to handle it (i.e. it's part of the function's logic) I will just return it untouched and let the consumer decide what to do (if anything at all).
> or placed untyped
That's not possible (at least in the typed languages I tried).
Making the real error types part of the signature of the method means that implementation changes require changing the signature, completely breaking encapsulation. In fact, I'd argue it makes polymorphism completely impossible.
Yeah, well, Java's semantics and programer laziness make a mess. News at 11.
> I'd argue it makes polymorphism completely impossible.
Oh, so two birds with one stone!
I don't see why.
Putting error conditions in the function signature means that the signature reflects the implementation details of the function. Change those implementation details and the signature changes, affecting the callers and their signatures. Unless you abstract the error and then you lose the only advantage of passing the errors around rather than using exceptions.
https://play.rust-lang.org/?version=stable&mode=debug&editio...
Let's add a new error condition:
https://play.rust-lang.org/?version=stable&mode=debug&editio...
Only the function raising the error, and the consumer, had to change (and the consumer only changed because it _cared_ about the error type!)
If you want an example with nested errors I can do it, you'll see it's mostly the same.
This isn't a realistic example at all because your MyError is simple enum that you changed to make it work. If you add another error, or have more than one level, or call it multiple locations, and don't eat the error then now you're changing many more functions, just as I said.
Well, that's what an error is. A simple enum of the error cases and maybe some metadata.
EDIT: e.g. IO errors in Rust's std: https://doc.rust-lang.org/src/std/io/error.rs.html#91-174
If your first guess is this isn't realistic because it's "too simple" then maybe you've never been exposed to this kind of error handling? It's so simple it's unbelievable?
> If you add another error
That's what I did, I added a new error.
> or have more than one level and don't eat the error now you're changing more methods, just as I said.
As I said, if you want an example with nested error types I can do it:
https://play.rust-lang.org/?version=stable&mode=debug&editio...
As you can see, caller() (which would be our "middleman" here, the one that neither produces nor uses the error) didn't change at all.
> If you had an external compiled library the consumed MyError or called caller, you just broke it, becuase you changed the type.
You mean a dynamically-linked library? Yep, completely broken, but that's not the only problem with DLLs. Static linking is fine.
Anyways, the contract changed, so it's a breaking change, so that's good in my book. The contract changed in the case of exceptions too, you just hand-waved over the issue and called it a day.
Wouldn't it be the same with exceptions across library boundaries?
I explained why the example is too simple. If you coded this in C with an integer error code the code would also be roughly the same size.
> Wouldn't it be the same with exceptions across library boundaries?
Actually no. That's the significant difference.
I can freely change the implementation and if that causes new and different exceptions to be possible, that doesn't affect the implementors. And most of the time such a change makes will make material affect on the implementors so they are broken for nothing.
And I offered a second example with a nested error, and nothing changed. Errors are just enums, there's nothing more complex than this. It's that simple.
> If you coded this in C with an integer error code the code would also be roughly the same size.
But it wouldn't check the error types. It's a zero-cost abstraction.
> I can freely change the implementation and if that causes new and different exceptions to be possible, that doesn't affect the implementors.
Neither does in my case... unless I care about the actual errors, which I did in this case in main() just for completeness.
What value is the enum value Other(OtherError) actually provide other than to satisfy the compiler? How far do we take this when there are more levels? OtherFailure(OtherMistake(Other(OtherError)))?
> Neither does in my case...
In each example you changed the implementation of type so that it is no longer compatible. You altered the signature of the method.
> But it wouldn't check the error types. It's a zero-cost abstraction.
It's funny that you say that as error returns have a non-zero runtime cost. Yet exceptions can actually be zero-cost at runtime (if they aren't thrown) as they can be implemented out of band. Only the most naive implementation propagates exceptions with multi-value returns.
Now I do understand why Rust is implemented this way but it's not because error returns are unequivocally better.
Good code with exceptions have very little code actually related to error handling. Throw often, catch infrequently, and use normal resource cleanup for exceptional cleanup as well. You can get far with a 2-3 well placed catch statements and that's it. No extra typing. No error propagation code.
If you think of error handling as happening where the error is raised as opposed to where it is handled it's hard to see the advantage of exceptions. Where the error is raised is not that important.
What's a language that does exceptions just right for you?
If I had to choose between checked exceptions and well typed error propagation, I'd choose the latter hands down. Checked exceptions have all the disadvantages of error returns and none of the advantages.
This meshes with my point about where the error concerns are; Java requires you to be very concerned about what errors are thrown at every method definition and call. But C#, which is a very similar language, doesn't. In C#, you only have to be concerned about where and what errors you can handle.
Exception bubbling is implicit.
There's no way to know which exceptions can be thrown from a function just by looking at its code, nor looking at the called-functions code. You have to keep in your head all the call chain up to the first function that does not call any other function (good luck with that).
Even worse: we don't even know _where_ the function can stop running and start unwinding. This leads to defensive programming. Hello try-catch soup!
It's a slippery slope that gets worse and worse with each abstraction layer.
Languages with typed errors have been nothing but a joy for me.