... function3
16 void function2() {
17 cout << "function3 is called inside function2" << endl;
18 function3(); // stack unwinding occurs, return control to function1
19 }
20
21 // function1 invokes function2
22 void function1() {
23 cout << "function2 is called inside function1" << endl;
24 function2(); // stack unwinding occurs, return control to main
25 }
26
27 // demonstrate stack unwinding
28 int main() {
29 // invoke function1
30 try {
31 cout << "function1 is called inside main" << endl;
32 function1(); // call function1 which throws runtime_error
33 }
34 catch (const runtime_error& error) { // handle runtime error
35 cout << "Exception occurred: " << error.what() << endl;
36 cout << "Exception handled in main" << endl;
37 }
38 }
function1 ...
Get C++ How to Program, 10/e now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.