weno
weno

Reputation: 856

How to emphasize that function may throw?

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;

Upvotes: 1

Views: 68

Answers (2)

bipll
bipll

Reputation: 11940

/**
 * @throws std::runtime_error
 */
void checkPlayerCounter();

Upvotes: 1

eerorika
eerorika

Reputation: 238441

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.

Upvotes: 4

Related Questions