← Back to Question List
C++ Quiz #157
The Variable the Failed Call Never Set
x starts at -1. The second call to risky throws during the assignment. What is printed?
📄 Code
1#include <iostream>2#include <stdexcept>34int risky(int n) {5 if (n < 0) {6 throw std::invalid_argument("negative");7 }8 return n * 2;9}1011int main() {12 int x = -1;13 try {14 x = risky(5);15 std::cout << "x is " << x << "\n";16 x = risky(-5);17 std::cout << "x is " << x << "\n";18 } catch (const std::exception&) {19 std::cout << "after the throw x is " << x << "\n";20 }21 return 0;22}