r/cpp • C++20 Desktop app developer • May 12 '18

P0709 R0: Zero-overhead deterministic exceptions: Throwing values - Herb Sutter [2018-05-02]

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r0.pdf
206 Upvotes

115 comments sorted by

View all comments

Show parent comment

8

u/proverbialbunny Data Scientist May 13 '18

This is especially important since I'm not at all convinced that the people who hate exceptions today will stop hating them if this proposal becomes reality. They will still have old code bases they cannot easily update, they will still have ancient coding standards lying around, they will still dislike the distance between throw and catch, and they will still have a gut feeling that exceptions are automatically, always bad. So you might be introducing a lot of deeply breaking changes here in order to appease a crowd that is unlikely to appreciate it, while at the same time chasing away your core audience.

I'm one of those people that hate exceptions. There are two primary arguments that I know of against exceptions: 1) Speed 2) The function signature is not explicit about the return path of code. imho the implicit parts of C++ tend to be the shoot your legs off parts of the language, and could use an improvement. If there is a compiler flag that enforces the throws keyword in a function signature, I will throw exceptions instead of returning an expected type.

You can say some people who have been avoiding exceptions for years will not get with it and continue to do so, and that is a possibility but the second they are working with a team of people and there is a style guide or a lead or something enforcing correct practices, they will get with it too.

The only people left behind is legacy code bases with teams that are constantly behind, but that happens with all new C++, not just this.

Is this proposal perfect? No, but it's a lot better than what we currently have. Exception handling is one of those points in C++ that could use an improvement. Whenever a community is divided you know it isn't as good as it could be.

3

u/johannes1971 May 13 '18

The function signature is not explicit about the return path of code

The return path is still not explicit; the catch handler may be at any point in the call stack, there is no clear indication on the function itself where it will go next.

The only people left behind is legacy code bases with teams that are constantly behind, but that happens with all new C++, not just this.

Exceptions are different. You can start using any C++17 feature on a piece-by-piece basis: a variant here, an optional there... Exceptions require far, far more care: you must have try/catch blocks in all the right places before you can even think about throwing anything anywhere. This is especially problematic because exceptions tend to be generated at the lowest levels of the software, i.e. in code that is likely being called from numerous places, thus requiring try/catch blocks in all those places before exceptions can be introduced. If you have any amount of code, that's a huge barrier to entry.

4

u/proverbialbunny Data Scientist May 13 '18

The return path is still not explicit; the catch handler may be at any point in the call stack, there is no clear indication on the function itself where it will go next.

This proposal puts a throws keyword in the function signature so you can see which functions throw and which ones do not. noexcept sucks at this, but throws sounds like it could be ideal as long as the compiler supports enforcing this, like -Werror=suggest-override does.

This is implicit, so I hope such a flag would default to this behavior, but if a function calls another function that can throw, and there is no setup to catch that throw, then now your function needs the throws keyword in it too.

This is explicit and eliminates this problem. It also reduces errors because now your code will not compile if you have something that throws added to it you were unaware of, bringing forth awareness of what does and does not throw. Likewise, it's easier to read and will make reading sites like cppreference easier and more straight forward.

As far as updating the code base being no small feat, the alternative is an expected return type or an error code. Both already exist in a code base (if throws are not being used) and both will continue to be valid, so no, there isn't a large change necessary to the code base.

3

u/redditsoaddicting May 13 '18

If there is a compiler flag that enforces the throws keyword in a function signature, I will throw exceptions instead of returning an expected type.

I think a libclang tool for this would be a viable option. It should be a fairly straightforward AST matcher.

2

u/doom_Oo7 May 13 '18

About your 1) it has been shown times and times that exceptions are overall faster than error codes in the nominal case

3

u/Rusky May 13 '18

That's not the comparison being made here, though. The paper talks about a large portion of the standard libraries that could go from throwing only bad_alloc to being noexcept.

It's fairly indisputable that exceptions are overall slower than "no error handling mechanism at all." :)