Validation functions can throw when an input is outside the range the program accepts.

validation Throwing from validation keeps the normal path simple and sends invalid inputs to a handler.

Validation Errors

score
validation_errors.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <stdexcept>

void validateScore(int score) {
    if (score < 0 || score > 100) {
        throw std::out_of_range("score");
    }
}

int main() {
    int score = 85;

    try {
        validateScore(score);
        std::cout << "score=" << score << std::endl;
        std::cout << "valid=1" << std::endl;
    } catch (const std::out_of_range& error) {
        std::cout << "score=" << score << std::endl;
        std::cout << "valid=0" << std::endl;
    }

    return 0;
}
#include <iostream>
#include <stdexcept>

void validateScore(int score) {
    if (score < 0 || score > 100) {
        throw std::out_of_range("score");
    }
}

int main() {
    int score = -5;

    try {
        validateScore(score);
        std::cout << "score=" << score << std::endl;
        std::cout << "valid=1" << std::endl;
    } catch (const std::out_of_range& error) {
        std::cout << "score=" << score << std::endl;
        std::cout << "valid=0" << std::endl;
    }

    return 0;
}
#include <iostream>
#include <stdexcept>

void validateScore(int score) {
    if (score < 0 || score > 100) {
        throw std::out_of_range("score");
    }
}

int main() {
    int score = 120;

    try {
        validateScore(score);
        std::cout << "score=" << score << std::endl;
        std::cout << "valid=1" << std::endl;
    } catch (const std::out_of_range& error) {
        std::cout << "score=" << score << std::endl;
        std::cout << "valid=0" << std::endl;
    }

    return 0;
}
  1. score ← 85

    10int main() {11    int score→ 85 = 85; //@score=-5, 120
  2. try

    13try {14    validateScore(score85);15    std::cout << "score=" << score << std::endl;
  3. void validateScore(int score)

    4void validateScore(int score85) {5    if (score < 0 || score > 100) {
  4. validateScore(score);

    13try {14    validateScore(score85);15    std::cout << "score=" << score85 << std::endl;16    std::cout << "valid=1" << std::endl;17} catch (const std::out_of_range& error) {
    outputscore=85
    valid=1
  5. return 0;

    22    return 0;23}
  1. score ← -5

    10int main() {11    int score→ -5 = -5;
  2. try

    13try {14    validateScore(score-5);15    std::cout << "score=" << score << std::endl;
  3. void validateScore(int score)

    4void validateScore(int score-5) {5    if (score < 0 || score > 100) {
  4. if (score < 0 || score > 100)

    4void validateScore(int score) {5    if (score-5 < 0 || score > 100) {6        throw std::out_of_range("score");7    }
  5. catch (const std::out_of_range& error)

    16    std::cout << "valid=1" << std::endl;17} catch (const std::out_of_range& error) {18    std::cout << "score=" << score << std::endl;19    std::cout << "valid=0" << std::endl;20}
    outputscore=-5
    valid=0
  6. return 0;

    22    return 0;23}
  1. score ← 120

    10int main() {11    int score→ 120 = 120;
  2. try

    13try {14    validateScore(score120);15    std::cout << "score=" << score << std::endl;
  3. void validateScore(int score)

    4void validateScore(int score120) {5    if (score < 0 || score > 100) {
  4. if (score < 0 || score > 100)

    4void validateScore(int score) {5    if (score120 < 0 || score > 100) {6        throw std::out_of_range("score");7    }
  5. catch (const std::out_of_range& error)

    16    std::cout << "valid=1" << std::endl;17} catch (const std::out_of_range& error) {18    std::cout << "score=" << score << std::endl;19    std::cout << "valid=0" << std::endl;20}
    outputscore=120
    valid=0
  6. return 0;

    22    return 0;23}