example1_declare_function.cpp

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

void safeFunction() noexcept {
    std::cout << "This function is noexcept and cannot throw exceptions." << std::endl;
}

void riskyFunction() {
    std::cout << "This function may throw an exception." << std::endl;
    throw std::runtime_error("Error in riskyFunction");
}

int main() {
    try {
        safeFunction();
        riskyFunction();
    } catch (const std::exception& e) {
        std::cerr << "Caught exception: " << e.what() << std::endl;
    }

    return 0;
}
Back to noexcept