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

The Catch-All Comes Last

easyExceptions

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <stdexcept>
3#include <string>
4 
5void attempt(int which) {
6 if (which == 1) {
7 throw std::runtime_error("disk full");
8 }
9 if (which == 2) {
10 throw 404;
11 }
12 if (which == 3) {
13 throw std::string("lost connection");
14 }
15 std::cout << "attempt " << which << " worked\n";
16}
17 
18int main() {
19 for (int which = 0; which <= 3; which++) {
20 try {
21 attempt(which);
22 } catch (const std::exception& e) {
23 std::cout << "standard: " << e.what() << "\n";
24 } catch (...) {
25 std::cout << "unknown problem\n";
26 }
27 }
28 return 0;
29}

🎯 Your Answer