CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #107

Three Levels Deep

easyExceptions

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <stdexcept>
3 
4void level3(int n) {
5 std::cout << "level3 start\n";
6 if (n == 0) {
7 throw std::runtime_error("n is zero");
8 }
9 std::cout << "level3 end\n";
10}
11 
12void level2(int n) {
13 std::cout << "level2 start\n";
14 level3(n);
15 std::cout << "level2 end\n";
16}
17 
18void level1(int n) {
19 std::cout << "level1 start\n";
20 level2(n);
21 std::cout << "level1 end\n";
22}
23 
24int main() {
25 try {
26 level1(1);
27 level1(0);
28 } catch (const std::runtime_error& e) {
29 std::cout << "main caught: " << e.what() << "\n";
30 }
31 std::cout << "main end\n";
32 return 0;
33}

🎯 Your Answer