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 #148

A return That Is Never Reached

mediumExceptions

share has a return inside its try block and another inside its catch block. What is printed?

📄 Code

1#include <iostream>
2#include <stdexcept>
3 
4int 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}
15 
16int 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}