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

Inner and Outer Handlers

mediumExceptions

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <stdexcept>
3 
4void process(int item) {
5 if (item == 2) {
6 throw std::invalid_argument("item 2 is damaged");
7 }
8 if (item == 3) {
9 throw std::runtime_error("belt stopped");
10 }
11 std::cout << "packed item " << item << "\n";
12}
13 
14int main() {
15 try {
16 for (int item = 1; item <= 4; item++) {
17 try {
18 process(item);
19 } catch (const std::invalid_argument& e) {
20 std::cout << "skipped: " << e.what() << "\n";
21 }
22 }
23 std::cout << "all items done\n";
24 } catch (const std::runtime_error& e) {
25 std::cout << "line halted: " << e.what() << "\n";
26 }
27 std::cout << "shift over\n";
28 return 0;
29}

🎯 Your Answer