Skip to content

New rule proposal: exception types should be nothrow copyable #1921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
szaszm opened this issue Jun 3, 2022 · 4 comments
Closed

New rule proposal: exception types should be nothrow copyable #1921

szaszm opened this issue Jun 3, 2022 · 4 comments
Assignees

Comments

@szaszm
Copy link

szaszm commented Jun 3, 2022

There is a similar rule in the SEI CERT C++ Coding Standard: ERR60-CPP. Exception objects must be nothrow copy constructible (down at the time of submitting this issue).

Rationale: exception objects are copy-initialized, and if an implementation chooses to not elide this copy, or there is an additional copy at the catch site (i.e. not following E.15: Throw by value, catch exceptions from a hierarchy by reference), and the copy constructor throws, then std::terminate is called.

It's sufficient if the exception type is nothrow copy constructible, but I can't see why someone would want to make a throwing copy assignment operator at the same time, aside from doing some magic in the copy constructor and not bothering to define a copy assignment operator, but that already violates the rule of three / five.

@N-Dekker
Copy link
Contributor

N-Dekker commented Jun 4, 2022

Would such a rule suggest using reference counting, internally, when the exception object contains dynamically allocated memory?

It's quite common for a user-defined exception class to have a dynamically allocated string, holding an error message.

@szaszm
Copy link
Author

szaszm commented Jun 4, 2022

Yes, a std::shared_ptr<const char[]> or a std::shared_ptr<const std::string> inside the exception object is one way of achieving this. I usually just derive from one of the standard exception types that already contains a message, and use its storage.
e.g.:

struct my_exception : std::runtime_error {
  using std::runtime_error::runtime_error;
};

@hsutter
Copy link
Contributor

hsutter commented Jun 13, 2022

Editors call: We think this is a good suggestion. We plan to extend E.16 to include exception type copy/move construction.

@hsutter hsutter self-assigned this Jun 13, 2022
@N-Dekker
Copy link
Contributor

@hsutter Thank you for addressing this issue, cool! Small nitpick, it now says at

- Try not to `throw` a type whose copy constructor is not `noexcept`. In general we cannot mechanically enforce this, because even `throw std::string(...)` could throw but does not in practice.

even throw std::string(...) could throw but does not in practice.

Sounds a bit interesting. Of course, throw does throw!

I guess you mean something like:

even when throwing an std::string, its copy-constructor could throw, but that is highly unlikely to happen in practice

Right? Would you like to fix it directly on the master branch, or would you rather have a pull request?

bgloyer added a commit to bgloyer/CppCoreGuidelines that referenced this issue Jun 16, 2022
* F.16 ("in" parameters): Move Matrix example to F.20 (return values) (isocpp#1922)

The `Matrix` example and the notes about assignment appear off-topic in rule F.16, as F.16 is specifically about "in" parameters.

With help from Sergey Zubkov.

* SL.io.50 (Avoid `endl`): Mention string streams (isocpp#1920)

Explicitly mentioned string streams as `endl` insertions into string streams do actually occur in the wild.

With help from Sergey Zubkov.

* Extended E.16 to include copy ctor for exception type, closes isocpp#1921

* Fix GitHub Actions build warnings, Marker style should be `*` (isocpp#1925)

* restored reference

* Added references to note

Co-authored-by: Niels Dekker <[email protected]>
Co-authored-by: Herb Sutter <[email protected]>
bgloyer added a commit to bgloyer/CppCoreGuidelines that referenced this issue Jul 9, 2022
* F.16 ("in" parameters): Move Matrix example to F.20 (return values) (isocpp#1922)

The `Matrix` example and the notes about assignment appear off-topic in rule F.16, as F.16 is specifically about "in" parameters.

With help from Sergey Zubkov.

* SL.io.50 (Avoid `endl`): Mention string streams (isocpp#1920)

Explicitly mentioned string streams as `endl` insertions into string streams do actually occur in the wild.

With help from Sergey Zubkov.

* Extended E.16 to include copy ctor for exception type, closes isocpp#1921

* Fix GitHub Actions build warnings, Marker style should be `*` (isocpp#1925)

* C.166: Use markdown format and document title for link (isocpp#1929)

* C.9: Improve an inline link (isocpp#1933)

Avoid ending the sentence with a dangling "see".

Co-authored-by: Niels Dekker <[email protected]>
Co-authored-by: Herb Sutter <[email protected]>
Co-authored-by: Francisco Moretti <[email protected]>
Co-authored-by: Sven van Haastregt <[email protected]>
bgloyer added a commit to bgloyer/CppCoreGuidelines that referenced this issue Jul 9, 2022
* Update CppCoreGuidelines.md

* Update CppCoreGuidelines.md

* Update isocpp.dic

* use snake casing

* sake case naming

* C 32 comments (#3)

* F.16 ("in" parameters): Move Matrix example to F.20 (return values) (isocpp#1922)

The `Matrix` example and the notes about assignment appear off-topic in rule F.16, as F.16 is specifically about "in" parameters.

With help from Sergey Zubkov.

* SL.io.50 (Avoid `endl`): Mention string streams (isocpp#1920)

Explicitly mentioned string streams as `endl` insertions into string streams do actually occur in the wild.

With help from Sergey Zubkov.

* Extended E.16 to include copy ctor for exception type, closes isocpp#1921

* Fix GitHub Actions build warnings, Marker style should be `*` (isocpp#1925)

* restored reference

* Added references to note

Co-authored-by: Niels Dekker <[email protected]>
Co-authored-by: Herb Sutter <[email protected]>

Co-authored-by: Niels Dekker <[email protected]>
Co-authored-by: Herb Sutter <[email protected]>
bgloyer added a commit to bgloyer/CppCoreGuidelines that referenced this issue Jul 9, 2022
* F.16 ("in" parameters): Move Matrix example to F.20 (return values) (isocpp#1922)

The `Matrix` example and the notes about assignment appear off-topic in rule F.16, as F.16 is specifically about "in" parameters.

With help from Sergey Zubkov.

* SL.io.50 (Avoid `endl`): Mention string streams (isocpp#1920)

Explicitly mentioned string streams as `endl` insertions into string streams do actually occur in the wild.

With help from Sergey Zubkov.

* Extended E.16 to include copy ctor for exception type, closes isocpp#1921

* Fix GitHub Actions build warnings, Marker style should be `*` (isocpp#1925)

* C.166: Use markdown format and document title for link (isocpp#1929)

* C.9: Improve an inline link (isocpp#1933)

Avoid ending the sentence with a dangling "see".

* C 32 (#5)

* Update CppCoreGuidelines.md

* Update CppCoreGuidelines.md

* Update isocpp.dic

* use snake casing

* sake case naming

* C 32 comments (#3)

* F.16 ("in" parameters): Move Matrix example to F.20 (return values) (isocpp#1922)

The `Matrix` example and the notes about assignment appear off-topic in rule F.16, as F.16 is specifically about "in" parameters.

With help from Sergey Zubkov.

* SL.io.50 (Avoid `endl`): Mention string streams (isocpp#1920)

Explicitly mentioned string streams as `endl` insertions into string streams do actually occur in the wild.

With help from Sergey Zubkov.

* Extended E.16 to include copy ctor for exception type, closes isocpp#1921

* Fix GitHub Actions build warnings, Marker style should be `*` (isocpp#1925)

* restored reference

* Added references to note

Co-authored-by: Niels Dekker <[email protected]>
Co-authored-by: Herb Sutter <[email protected]>

Co-authored-by: Niels Dekker <[email protected]>
Co-authored-by: Herb Sutter <[email protected]>

Co-authored-by: Niels Dekker <[email protected]>
Co-authored-by: Herb Sutter <[email protected]>
Co-authored-by: Francisco Moretti <[email protected]>
Co-authored-by: Sven van Haastregt <[email protected]>
hsutter added a commit that referenced this issue Jul 13, 2022
* Update CppCoreGuidelines.md

* Update CppCoreGuidelines.md

* Update isocpp.dic

* use snake casing

* sake case naming

* C 32 comments (#3)

* F.16 ("in" parameters): Move Matrix example to F.20 (return values) (#1922)

The `Matrix` example and the notes about assignment appear off-topic in rule F.16, as F.16 is specifically about "in" parameters.

With help from Sergey Zubkov.

* SL.io.50 (Avoid `endl`): Mention string streams (#1920)

Explicitly mentioned string streams as `endl` insertions into string streams do actually occur in the wild.

With help from Sergey Zubkov.

* Extended E.16 to include copy ctor for exception type, closes #1921

* Fix GitHub Actions build warnings, Marker style should be `*` (#1925)

* restored reference

* Added references to note

Co-authored-by: Niels Dekker <[email protected]>
Co-authored-by: Herb Sutter <[email protected]>

Co-authored-by: Niels Dekker <[email protected]>
Co-authored-by: Herb Sutter <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants