← Back to Question List
C++ Code Walkthrough #111
The Catch-All Comes Last
📄 Code
Read carefully — what does this print?1#include <iostream>2#include <stdexcept>3#include <string>45void 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}1718int 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}