CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #108

The Assignment That Never Happens

easyExceptions

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <stdexcept>
3 
4int halve(int value) {
5 if (value % 2 != 0) {
6 throw std::invalid_argument("odd number");
7 }
8 return value / 2;
9}
10 
11int main() {
12 int result = -1;
13 try {
14 result = halve(10);
15 std::cout << "result is " << result << "\n";
16 result = halve(7);
17 std::cout << "result is " << result << "\n";
18 } catch (const std::invalid_argument& e) {
19 std::cout << "error: " << e.what() << "\n";
20 }
21 std::cout << "final result " << result << "\n";
22 return 0;
23}

🎯 Your Answer