Skip to content
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

Code quality: functions in main() should not throw exceptions #367

Open
krishnakumarg1984 opened this issue Jun 21, 2023 · 0 comments
Open

Comments

@krishnakumarg1984
Copy link
Collaborator

krishnakumarg1984 commented Jun 21, 2023

C++'s exception mechanism is used to propagate error-information from deep down the call stack to upwards where it is to be caught by one of the calling functions within the project.

However, if main() doesn't catch the exception, it gets propagated further upwards to the OS, which is not considered a best practice.

This issue affects several example, benchmarking and test code files in the project.

Explanation/Background Information

As a general practice, the functions which should not throw exceptions are the following:

  • Destructors
  • Move constructors
  • Move assignment operators
  • The main() functions
  • swap() functions
  • Functions marked with throw() or noexcept

A destructor throwing an exception may result in undefined behavior, resource leaks or unexpected termination of the program. Throwing move constructor or move assignment also may result in undefined behavior or resource leak. The swap() operations expected to be non throwing most of the cases and they are always possible to implement in a non throwing way. Non throwing swap() operations are also used to create move operations. A throwing main() function also results in unexpected termination.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant