← Back to Question List
C++ Code Walkthrough #115
Any Type Can Be Thrown
📄 Code
Read carefully — what does this print?1#include <iostream>2#include <string>34void signal(int level) {5 if (level == 1) {6 throw 42;7 }8 if (level == 2) {9 throw std::string("red alert");10 }11 if (level == 3) {12 throw 2.5;13 }14 std::cout << "level " << level << " is calm\n";15}1617int main() {18 for (int level = 0; level <= 3; level++) {19 try {20 signal(level);21 } catch (int code) {22 std::cout << "int thrown: " << code << "\n";23 } catch (const std::string& message) {24 std::cout << "string thrown: " << message << "\n";25 } catch (...) {26 std::cout << "something else thrown\n";27 }28 }29 return 0;30}