example1_basic_usage.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <stdexcept>
#include <string>

void validateAge(int age) {
    if (age < 0 || age > 150) {
        throw std::runtime_error("Invalid age value");
    }
    std::cout << "Age is valid: " << age << std::endl;
}

int main() {
    try {
        validateAge(25);  // This should work fine
        validateAge(-5);  // This should throw an exception
    }
    catch (const std::runtime_error& e) {
        std::cerr << "Caught a runtime_error: " << e.what() << std::endl;
    }

    return 0;
}
Back to std_runtime_error