⚠️ 抓住它,并且问出原因

👁️ 1 人浏览 💬 0 人评论 ❤️ 添加收藏

(本条路线统一用 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;
}
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论