If you compile exception handling to separate return addresses this becomes rather obvious. In this case your control flow also stays reducible, which is commonly taken to mean "simple to reason about", and is very different from the kind of control flow that you can introduce using goto statements.
I think the main problems with exception handling are cultural and psychological. Culturally we don't insist on proper documentation for exceptions. At best you'll see documentation for the functions throwing exceptions directly, but almost never for the functions using them a few levels up the stack... The situation is even worse with higher order functions for which people almost never document the exception behavior. Psychologically most people seem to ignore all but the most obvious exceptional cases. We really have to force ourselves to notice everything that could go wrong and to take a step back and rethink our designs if this question doesn't have a simple answer...
As a for instance, I can give instruction to my team to document exceptions when they are used, but during code review, I cannot guarantee that my engineers are being thorough enough to also document exceptions thrown deeper in the stack -- especially when dealing with a very large pre-existing codebase they didn't write and only have a passing familiarity with.
But then, exchanging that for return values does not make your life easier in any way. It just means you will have to write redundant tests, and will have a flow that is exactly equivalent to the exceptions flow, after you simplify the redundancy down.
You can have your compiler or IDE making the list of possible exceptions for you. But that list is always too big to reason about anyway, and for high order functions, it may be the entire set of exceptions available for your code and some more the it can't discover at compile time.
Haskell has tried a different take on the problem with error monads. It's clean, simple, and does not solve the entire problem. So, it also has exceptions, and they are even worse there than at the imperative languages, because everything is high order.
I think people are entirely justified in ignoring the problem and moving on. Unless you want to dedicate yourself into research, there is not a clear path to solve it.
Unchecked exceptions subvert the type system, so they are convenient because they are cheating when compared with Result<T,E> types.
Result<T,E> types are the one feature that have me thinking of moving from C++ to Rust. A lot of my experimental C++ code with callbacks looks like a bit like this now:
template <OnSuccess, OnFailure> void someFunction(..., OnSuccess onSuccess, OnFailure onFailure) { ...
Using the above style:
1. Allows error handling to be type-checked 2. Uses the heap less
But it's very clunky! If anybody has a better way to achieve these two things in C++ I'd like to know ...
Edit:
Erlang, which is known for its reliability, gets this right: kill the whole Erlang process in a clean fashion so that we can get transactional semantics. (It's not that performant though.)
This is also the rust way - panics kill the thread they occur in.
The next improvements will be custom error types (I had it done but reverted in this code) and a error_chain like feature (started testing and forgot to remove some not working code there).
[1] https://github.com/fungos/sllab/blob/master/src/viewer.cpp#L...
I don't fully understand your code yet: Is it the case that the type-safety of your approach relies on the caller honouring the convention of accessing Result<T,E> via the try and try_chain macros?
I really need to look in detail at how Rust enforces type-safety of Result<T,E>. One thing that I like about how algebraic data types work in functional languages (e.g. Either in Haskell) is that they eliminate entirely the need for null pointers completely and the possibility of derefencing them (Tony Hoare's billion dollar mistake) and I had assumed that Rust would give me the same.
You can see somewhere there I do this:
return std::move(ArgsResult Ok(options));
I could also have done: ArgsResult func() { ... return Ok(options); }
In this example there are two instantiations of the type Result<T, E>: typedef Result<ProgramOptions, Error> ArgsResult;
typedef Result<bool, Error> ProgramResult;
(Note that the typedefs aren't needed, but I prefer to be explicit there.)The try macro is just trying to emulate try! and it is not really needed. We may access the result by hand and the correct type will be used, but it is way uglier (see: https://github.com/fungos/sllab/blob/master/src/viewer.cpp#L...) due the access on the struct members .e (error) or .v (value). And if I try to assign .v to another type, the compiler will give a compile time error.
And probably would be better to use std::variant there as it is more like a tagged union, I just haven't tried to improve on it anymore.
Also, note that my unwrap doesn't panic, I should put a test/assert there...
Anyway, the poc is there and it should possible to have something almost as clean as Rust with some more work.
Basically, you can't use it as a T or an E; you have to process it in some way first. Basically, if you're familiar with Either, you know the whole story, it is the same. We originally had Either, then someone made Result, people always reached for Result over Either, so at some point, we axed Either. There is https://crates.io/crates/either though too.
> I had assumed that Rust would give me the same
They do. ADTs in Rust are very similar to languages like Haskell. The only major difference I can think of is everything is stored inline in the enums in Rust (they're what one might call "value types" in some languages) without any allocations required.
i.e. a parent function needs to expose an error type that is a base class of all errors that can occur the methods being called, right ?
and i wondering how you would get stack traces from these errors ?
I've tried using this and my own implementation of Result<T,E> based on Alexandrescu's original talk, and both are significantly slower that C error codes or exceptions using the test program in the OP.
https://ericlippert.com/2014/03/03/living-with-unchecked-exc...
I'm not really a fan of checked exceptions either for the reasons that Anders Hejlsberg outlines in the interview.
However, the bit where he writes:
"I'm a strong believer that if you don't have anything right to say, or anything that moves the art forward, then you'd better just be completely silent and neutral, as opposed to trying to lay out a framework."
Isn't that exactly what has happened with unchecked exceptions in C++ and C#? They've added complexity compared with C style error codes, without actually moving the art forward.
At a guess, destructors would reduce the advantage of exceptions in the case of deep stacks.
Not to mention, that's old-school error handling, Result<T,E> types are what the kids seem into these days.
Curious how destructors will affect the numbers.