Question 8
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <iostream>
#include <string>
int check(int x) {
if (x < 0) throw -1;
if (x == 0) throw std::string("zero");
if (x > 100) throw 'B';
return x;
}
int main() {
for (int v : {5, -3, 0, 500}) {
try { std::cout << check(v) << ' '; }
catch (int code) { std::cout << "int" << code << ' '; }
catch (const std::string& s) { std::cout << s << ' '; }
catch (...) { std::cout << "other "; }
}
std::cout << '\n';
return 0;
}
- A 5 int-1 zero other Correct
- B 5 int-1 zero int66
- C 5 -1 zero B
- D 5 int-1 other other
Correct Answer: Option A
Catch matching does not apply arithmetic conversions: a thrown char is not caught by catch (int), so it reaches catch (...).
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!