example3_exception_handling.cpp

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

void terminate_handler() {
    std::cout << "Custom terminate handler called" << std::endl;
    std::abort();
}

void throw_unexpected() noexcept(false) {
    throw std::runtime_error("Unexpected exception");
}

int main() {
    std::set_terminate(terminate_handler);

    try {
        throw_unexpected();
    } catch (const std::runtime_error& e) {
        std::cout << "Caught runtime_error exception: " << e.what() << std::endl;
    } catch (...) {
        std::cout << "Caught unknown exception" << std::endl;
    }

    return 0;
}
Back to cxxabi.h