蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

验证除零异常、多分支/catch(...) 匹配、标准异常族谱及自定义 std::exception 子类

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-exceptions-handling.cpp
explore-cpp-exceptions-handling.cpp112 行
#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
#include <typeinfo>
#include <vector>

static double safe_division(int a, int b) {
    if (b == 0) {
        throw "Division by zero condition!";
    }
    return static_cast<double>(a) / static_cast<double>(b);
}

struct MyException : public std::exception {
    const char* what() const noexcept override {
        return "C++ Exception";
    }
};

struct PolyBase {
    virtual ~PolyBase() = default;
};

struct PolyDerived : public PolyBase {};

static std::string dispatch_catch(int mode) {
    try {
        if (mode == 1) throw 404;
        if (mode == 2) throw std::invalid_argument("bad_arg");
        throw 3.14;
    } catch (int code) {
        return "int:" + std::to_string(code);
    } catch (const std::invalid_argument& e) {
        return std::string("inv:") + e.what();
    } catch (...) {
        return "catch_all";
    }
}

int main() {
    // 1. Throw & catch const char* on divide-by-zero
    std::string div_err;
    double normal_val = safe_division(50, 5);
    try {
        (void)safe_division(50, 0);
    } catch (const char* msg) {
        div_err = msg;
    }
    std::string act1 = std::to_string(static_cast<int>(normal_val)) + "|" + div_err;
    const std::string exp1 = "10|Division by zero condition!";
    bool ok1 = (act1 == exp1);

    // 2. Multiple catch blocks & catch(...) fallback
    std::string act2 = dispatch_catch(1) + "," + dispatch_catch(2) + "," + dispatch_catch(3);
    const std::string exp2 = "int:404,inv:bad_arg,catch_all";
    bool ok2 = (act2 == exp2);

    // 3. Standard exception hierarchy (out_of_range, invalid_argument, bad_cast)
    int std_caught = 0;
    try {
        std::vector<int> v = {1, 2};
        (void)v.at(5);
    } catch (const std::out_of_range& e) {
        if (std::string(e.what()).size() > 0) ++std_caught;
    }
    try {
        (void)std::stoi("not_a_number");
    } catch (const std::invalid_argument&) {
        ++std_caught;
    }
    try {
        PolyBase base;
        (void)dynamic_cast<PolyDerived&>(base);
    } catch (const std::bad_cast&) {
        ++std_caught;
    }
    bool ok3 = (std_caught == 3);

    // 4. Custom exception inheriting std::exception & overriding what()
    std::string direct_msg;
    std::string poly_msg;
    try {
        throw MyException();
    } catch (const MyException& e) {
        direct_msg = e.what();
    }
    try {
        throw MyException();
    } catch (const std::exception& e) {
        poly_msg = e.what();
    }
    std::string act4 = direct_msg + "|" + poly_msg;
    const std::string exp4 = "C++ Exception|C++ Exception";
    bool ok4 = (act4 == exp4);

    std::cout << "{\n"
              << "  \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
              << "  \"cases\": [\n"
              << "    {\"name\": \"throw_catch_div_zero\", \"expected\": \"" << exp1
              << "\", \"actual\": \"" << act1 << "\", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
              << "    {\"name\": \"multi_catch_and_ellipsis\", \"expected\": \"" << exp2
              << "\", \"actual\": \"" << act2 << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
              << "    {\"name\": \"std_exception_hierarchy\", \"expected\": 3, \"actual\": "
              << std_caught << ", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
              << "    {\"name\": \"custom_exception_what\", \"expected\": \"" << exp4
              << "\", \"actual\": \"" << act4 << "\", \"passed\": " << (ok4 ? "true" : "false") << "}\n"
              << "  ]\n"
              << "}\n";
    return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。

入口文件:projects/cplusplus-cpp-exceptions-handling-lab/explore-cpp-exceptions-handling.cpp

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-cpp-exceptions-handling.cpp,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C++

课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip

下载课程关联工程包:C++

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2323.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习