News for hackers. Stuff that matters.
Re:Measuring execution performance of C++ exceptions vs. err... by arcticbull on Monday January 09, 2017 @06:09AM (#13355493)
IMO the reason not to use them isn't performance or size (although those were legitimate factors in the past AFAIK) -- rather that they make control flow very difficult to reason about. They're worse than a go-to statement, they're a comes-from statement. They can pop out of anywhere and be handled anywhere up the stack. They always seem like a good idea at the time, and end up biting you in the butt.

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.

Re:Measuring execution performance of C++ exceptions vs. err... by fmap on Monday January 09, 2017 @07:37AM (#13355883)
From a reasoning perspective throwing an exception is the same as returning from a function. If you want to reason about code which potentially throws an exception you have to keep track of a separate exception post condition.

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...

Re:Measuring execution performance of C++ exceptions vs. err... by mondoshawan on Monday January 09, 2017 @09:15AM (#13356397)
Documenting and mentally reasoning about exceptions is only half the problem. The other half is scaling this discipline inside individual teams and scaling it out beyond as well.

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.

Re:Measuring execution performance of C++ exceptions vs. err... by marcosdumay on Monday January 09, 2017 @11:47AM (#13357634)
Even Java had to create some escape valve for its explicit exceptions, just because there was a huge set of them that could appear literally anywhere.

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.

Re:Measuring execution performance of C++ exceptions vs. err... by baq on Monday January 09, 2017 @09:17AM (#13356416)
documenting every possible exception becomes next to impossible if any function above in the stack decides to throw a new kind after a code change...
Re:Measuring execution performance of C++ exceptions vs. err... by delhanty on Monday January 09, 2017 @07:55AM (#13355985)
+1 for this!

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.)

Re:Measuring execution performance of C++ exceptions vs. err... by staticassertion on Monday January 09, 2017 @08:32AM (#13356175)
> 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.

Re:Measuring execution performance of C++ exceptions vs. err... by delhanty on Monday January 09, 2017 @08:46AM (#13356245)
Thanks - that's good to know! Another reason for me to try rust then ...
Re:Measuring execution performance of C++ exceptions vs. err... by fungos on Monday January 09, 2017 @12:27PM (#13357950)
Just for fun, really, I've done a Result impl in my lab code[1]. I'm willing to improve it just to see how far I can get with it. :)

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...

Re:Measuring execution performance of C++ exceptions vs. err... by delhanty on Monday January 09, 2017 @07:36PM (#13361434)
Thanks for this.

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.

Re:Measuring execution performance of C++ exceptions vs. err... by fungos on Monday January 09, 2017 @10:28PM (#13362319)
The type safety of my sample is based on the auto deduction of the types at compile time.

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.

Re:Measuring execution performance of C++ exceptions vs. err... by steveklabnik on Monday January 09, 2017 @10:25PM (#13362308)
> I really need to look in detail at how Rust enforces type-safety of Result<T,E>.

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.

Re:Measuring execution performance of C++ exceptions vs. err... by dbaupp on Monday January 09, 2017 @11:15PM (#13362521)
Result is exactly Haskell's Either, just with a different name.

> 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.

Re:Measuring execution performance of C++ exceptions vs. err... by pixel_fcker on Monday January 09, 2017 @02:42PM (#13359148)
There's this: https://github.com/ptal/expected which seems to work pretty well in my tests so far, though I haven't benchmarked it yet. The default implementation uses exceptions as the error type but you can supply your own type (e.g. an int, string, or a wrapped version of either or both) trivially.
Re:Measuring execution performance of C++ exceptions vs. err... by je42 on Monday January 09, 2017 @02:57PM (#13359279)
just looked at this, nice! but i wasn't able to find how in the document how to propagate errors up the stack ?

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 ?

Re:Measuring execution performance of C++ exceptions vs. err... by pixel_fcker on Wednesday January 11, 2017 @02:34PM (#13376768)
Best just to use std::exception as the base class and derive from that, then if you want to get a stack trace just rethrow the contained exception.

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.

Re:Measuring execution performance of C++ exceptions vs. err... by delhanty on Monday January 09, 2017 @07:36PM (#13361443)
Thanks for the link. I'll check it out and see whether it works for me.
Re:Measuring execution performance of C++ exceptions vs. err... by sidlls on Monday January 09, 2017 @01:15PM (#13358367)
It's fairly straightforward to have a rust-Result construct in C++. It's a bit more tedious if you want to make it a proper tagged union, but not inherently something that can't be done with a minimum of code and be as safe as possible.
Re:Measuring execution performance of C++ exceptions vs. err... by Ace17 on Monday January 09, 2017 @03:27PM (#13359490)
In case you haven't read it: The Trouble With Checked Exceptions ( http://www.artima.com/intv/handcuffs.html )
Re:Measuring execution performance of C++ exceptions vs. err... by delhanty on Monday January 09, 2017 @07:47PM (#13361522)
Thanks for the link. I hadn't read the interview, though I had read the following by Eric Lippert where he references it:

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.

Re:Measuring execution performance of C++ exceptions vs. err... by saurik on Monday January 09, 2017 @06:51AM (#13355664)
That result object is implemented in the same manner to an error code: you are burning cycles and registers on something that usually doesn't come up... all so you can clutter your code with what effectively amounts to a manual implementation of exception handling :/.
Re:Measuring execution performance of C++ exceptions vs. err... by arcticbull on Monday January 09, 2017 @07:02AM (#13355711)
That's fair, though in my opinion it's more important to be clear and explicit in what's happening both for your own sake and for your future co-workers so they can understand what's happening and why than worrying about some minor (per the article) performance issues.
Re:Measuring execution performance of C++ exceptions vs. err... by nick_ on Monday January 09, 2017 @11:06AM (#13357306)
That's what exceptions are for. They are a tool that _can_ be used to cut down on noise in code. Being clear and explicit about everything will leave your code looking like an implementation of the STL. The art of programming is partly about achieving a good signal-to-noise ratio.
Re:Measuring execution performance of C++ exceptions vs. err... by mcguire on Monday January 09, 2017 @11:27AM (#13357493)
Destructors make function returns more expensive; exceptions are faster in these examples because they elide returns.

At a guess, destructors would reduce the advantage of exceptions in the case of deep stacks.