← Back to Question List
C++ Code Walkthrough #108
The Assignment That Never Happens
📄 Code
Read carefully — what does this print?1#include <iostream>2#include <stdexcept>34int halve(int value) {5 if (value % 2 != 0) {6 throw std::invalid_argument("odd number");7 }8 return value / 2;9}1011int 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}