← Back to Question List
C++ Quiz #148
A return That Is Never Reached
share has a return inside its try block and another inside its catch block. What is printed?
📄 Code
1#include <iostream>2#include <stdexcept>34int share(int total, int people) {5 try {6 if (people == 0) {7 throw std::runtime_error("nobody to share with");8 }9 return total / people;10 } catch (const std::exception& e) {11 std::cout << "handled: " << e.what() << "\n";12 return -1;13 }14}1516int main() {17 int first = share(20, 4);18 std::cout << first << "\n";19 int second = share(20, 0);20 std::cout << second << "\n";21 return 0;22}