GESP C++ 真题 · 逐题精解
首页C++四级真题 › 2024年9月 › 第15题

GESP 2024年9月 C++四级 单选题 第15题

C++四级单选题2024年9月第15题

所属知识点:递推与递归 难度要求:掌握 考频:—

运行下面的代码,屏幕上将输出( )。
#include <iostream>
using namespace std;
int divide(int a, int b) {
if (b == 0) {
throw runtime_error("division by zero error ");
}
return a / b;
}
int main() {
int x = 10;
int y = 0; // 设为 0 会导致除零错误
try {
int result = divide(x, y);
cout << "result: " << result << endl;
} catch (const runtime_error& e) {
cout << "caught an exception: " << e.what() << endl;
}
return 0;
}

正确答案:C

题目解析
divide(10,0) 抛出异常,被 catch 捕获,只输出 caught an exception: division by zero error(try 里的 result 不会输出),选 C。

想系统刷完 GESP C++ 1~8 级真题,并查看每道题的逐题精讲?

进入 GESPPASS 开始练习