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

The Return That Never Returns

mediumExceptions

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <stdexcept>
3#include <string>
4 
5std::string grade(int score) {
6 try {
7 if (score > 100) {
8 throw std::out_of_range("score above 100");
9 }
10 if (score >= 50) {
11 return "pass";
12 }
13 return "fail";
14 } catch (const std::out_of_range& e) {
15 std::cout << "error: " << e.what() << "\n";
16 return "invalid";
17 }
18}
19 
20int main() {
21 int scores[] = {80, 120, 30};
22 for (int score : scores) {
23 std::string result = grade(score);
24 std::cout << score << ": " << result << "\n";
25 }
26 return 0;
27}

🎯 Your Answer