← Back to Question List
C++ Code Walkthrough #109
Which Catch Block Runs
📄 Code
Read carefully — what does this print?1#include <iostream>2#include <stdexcept>34void risky(int code) {5 if (code == 1) {6 throw std::invalid_argument("bad argument");7 }8 if (code == 2) {9 throw std::runtime_error("something failed");10 }11 std::cout << "code " << code << " is fine\n";12}1314int main() {15 for (int code = 0; code <= 2; code++) {16 try {17 risky(code);18 } catch (const std::invalid_argument& e) {19 std::cout << "invalid_argument: " << e.what() << "\n";20 } catch (const std::exception& e) {21 std::cout << "exception: " << e.what() << "\n";22 }23 }24 return 0;25}