CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List

Select topics to narrow your question pool, then enable Random to jump to a random question matching your filters.

Topics:
C++ Quiz #157

The Variable the Failed Call Never Set

hardExceptions

x starts at -1. The second call to risky throws during the assignment. What is printed?

📄 Code

1#include <iostream>
2#include <stdexcept>
3 
4int risky(int n) {
5 if (n < 0) {
6 throw std::invalid_argument("negative");
7 }
8 return n * 2;
9}
10 
11int 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}