⚠️ 抓住它,并且问出原因
(本条路线统一用 g++ -std=c++17 -O0 -Wall 编译)
#include <iostream>
#include <stdexcept>
#include <string>
static int div_of(int a, int b) {
if (b == 0) throw std::invalid_argument("zero");
return a / b;
}
int main() {
int ok = div_of(10, 2);
int caught = 0;
std::string msg;
try {
div_of(10, 0);
} catch (const std::invalid_argument &e) {
caught = 1;
msg = e.what();
}
std::cout << ok << "/" << caught << "/" << msg << "\n";
return 0;
}
全部评论