Stack Unwinding, throws an exception from a deeply nested function - C++ Statement

C++ examples for Statement:try catch

Description

Stack Unwinding, throws an exception from a deeply nested function

Demo Code

#include <iostream>
#include <stdexcept>

bool function3() {
    std::cout << "function3 called" << std::endl;
    return (true != true) ? true : throw std::runtime_error("function3");
}
bool function2() {
    std::cout << "function2 called" << std::endl;
    return false;
}
bool function1() {
    std::cout << "function1 called" << std::endl;
    return function2() == function3();
}

int main(int argc, const char *argv[]) {
    try {/*w  ww .j av a2s  .  c o m*/
        function1();
    } catch (std::runtime_error &e) {
        std::cout << "Exception occured: " << e.what() << std::endl;
    }

    return 0;
}

Result


Related Tutorials