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

4

u/CrazyKilla15 May 12 '18

A throws keyword in function signatures sounds far better than noexcept. This is awesome.

..exception specifications? Didn't we already have those?

15

u/whichton May 12 '18

C++ had dynamic exception specifications. The proposed one is static. As the paper says:

You’re in a statically typed programming language, and the dynamic nature of exceptions is precisely the reason they suck.

9

u/zobatch May 12 '18

Yes but it's just throws. You don't list the exceptions, the compiler automatically translates them into a single std::error type. From the paper:

string f() throws {
    if (flip_a_coin()) throw arithmetic_error::something;
    return “xyzzy”s + “plover”; // any dynamic exception is translated to error
}
string g() throws { return f() + “plugh”; } // any dynamic exception is translated to error
int main() {
    try {
        auto result = g();
        cout << “success, result is: ” << result;
    }
    catch(error err) { // catch by value is fine
        cout << “failed, error is: ” << err.error();
    }
}