Skip to content

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

@krishnakumarg1984

Description

@krishnakumarg1984

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions