CppCoreGuidelines icon indicating copy to clipboard operation
CppCoreGuidelines copied to clipboard

I.10 "Use exceptions..." doesn't mention [[nodiscard]] alternative

Open amatveiakin opened this issue 5 years ago • 2 comments

The reason as stated in I.10: Use exceptions to signal a failure to perform a required task is as follows:

Reason It should not be possible to ignore an error because that could leave the system or a computation in an undefined (or unexpected) state. This is a major source of errors.

There's another rule that satisfies the statement above, namely: Always use a type marked as [[nodiscard]] such as absl::Status or absl::StatusOr to signal a failure to perform a required task

Could you expand I.10 to explain why exceptions are preferable to this approach?

amatveiakin avatar Nov 09 '20 18:11 amatveiakin

Link for convenience: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i10-use-exceptions-to-signal-a-failure-to-perform-a-required-task And Status/StatusOr documentation: https://abseil.io/docs/cpp/guides/status

shaneasd avatar Nov 10 '20 14:11 shaneasd

I.10 makes a distinction between failing and returning a status (..."to report what are really status codes, rather than errors.").

Non-ignorable status codes are pretty cool though, and could have a mention here where it talks about "consider using a style that returns a pair of values" or in E.27: If you can’t throw exceptions, use error codes systematically

cubbimew avatar Nov 11 '20 21:11 cubbimew