Consider the following function:
void checkPlayerCounter() {
if (playerCounter != consts::numberOfPlayers) {
throw std::runtime_error("Detected " + std::to_string(playerCounter)
+ " player instead of " + std::to_string(consts::numberOfPlayers));
}
}
The function may throw, and for readibility I want to emphasize that in the function's declaration inside header file. How can I do that?
I am looking for something like:
void checkPlayerCounter() may throw;
You can use noexcept(false)
explicitly. Right where you suggested:
void checkPlayerCounter() noexcept(false);
Unfortunately, there is no way to avoid the double negative with noexcept
. Another option is a comment:
void checkPlayerCounter(); // may throw
That said, whoever reads the declaration should always assume that the function may throw, unless specified otherwise.