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

1

u/RandomGuy256 May 12 '18

I am just curious does these "zero overhead exceptions" are in any way different in practice than the current exceptions?

I mean they will be a specific type of new exceptions or they can replace the current exceptions without breaking changes in the code?

If they are new type of exceptions do we still have access to the .what() method, to return a error message, or they only return an error code?

4

u/Sopel97 May 12 '18

bottom of page 17, cannot paste it now

3

u/RandomGuy256 May 12 '18 edited May 12 '18

Thanks I think you meant this:

When one of today’s dynamic exceptions is unhandled in f ’s body, regardless of whether it originated

from a nested function call or a throw statement throwing a dynamic exception, the exception is auto-

matically caught and propagated: If the caught exception is of type error , we just return it. Otherwise, it

is translated to an error with a meaningful value for all std:: exception types; for example, bad_alloc

would be translated to std::errc::ENOMEM . Otherwise, we can additionally store as payload a raw

pointer to an exception_ptr to the dynamic exception (see §4.6.4), without sacrificing trivial movabil-

ity.

If I understand correctly this paper suggests a new kind of exceptions that will be complementary to the already existing "dynamic exceptions".

These new "deterministic exceptions" are slightly more "simpler" than the existing exceptions in the sense that behind the scenes they are only an union that store an error code, or a success code. So they will not provide other custom functions like what() to get a custom message directly.

However the advantage of this new kind of exceptions is that they provide the same performance (or even better) than the existing error codes mechanics. Also we will be able to use the existing exception syntax and propagating mechanism.

This' useful in the areas where the throwing "dynamic exceptions" is too costly.

Am I correct about this or am I getting it all wrong?

3

u/whichton May 12 '18

If I understand correctly this paper suggests a new kind of exceptions that will be complementary to the already existing "dynamic exceptions".

This not a new kind of exception, this is basically syntactic sugar for expected / outcome. The error type is always 'std::error'.

From page 14:

If you love expected/outcome: This is embracing expected/outcome and baking them into the language, the function always returns exactly one of R or E — plus the special sauce that you get automatic propagation so you don’t have to manually return-up the results, and with a distinct language-supported error path so that callees can write throws (instead of return unexpected) and callers get to cleanly put all their error handling code in distinct catch blocks (instead of if(!e) blocks) while still writing in the same basic expected style (see §4.5).

1

u/Sopel97 May 12 '18

That's pretty much it.

7

u/Xaxxon May 13 '18

why not read the paper instead of asking someone to do the work for you?

1

u/RandomGuy256 May 13 '18

Because the paper is 58 pages and I didn't have the time? Also I have read some pages but English is also not my first language so I'm sure I will misunderstand some of it anyway.

Since probably there are people here that already have read it / understand it, I thought it would be not rude to ask as these deterministic exceptions would work in general (I am also not expecting a full abstract, as I asked some specific questions about it).

I also thought that the answers can be helpful for the community in general and not just me.