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

实测 <stdexcept> 全部 9 个标准异常类、继承关系、非抽象特性与自定义派生异常

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

namespace lab {

struct Case {
    std::string name;
    std::string expected_json;
    std::string actual_json;
    bool passed;
};

std::string esc(const std::string& s) {
    std::string out = "\"";
    for (unsigned char c : s) {
        switch (c) {
            case '\"': out += "\\\""; break;
            case '\\': out += "\\\\"; break;
            case '\n': out += "\\n"; break;
            case '\r': out += "\\r"; break;
            case '\t': out += "\\t"; break;
            default:
                if (c < 0x20) {
                    char buf[8];
                    std::snprintf(buf, sizeof(buf), "\\u%04x", c);
                    out += buf;
                } else {
                    out += static_cast<char>(c);
                }
        }
    }
    out += "\"";
    return out;
}

std::string jbool(bool v) { return v ? "true" : "false"; }

void emit(const std::vector<Case>& cases) {
    std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
    for (std::size_t i = 0; i < cases.size(); ++i) {
        if (i) std::cout << ", ";
        std::cout << "{\"name\": " << esc(cases[i].name)
                  << ", \"expected\": " << cases[i].expected_json
                  << ", \"actual\": " << cases[i].actual_json
                  << ", \"passed\": " << jbool(cases[i].passed) << "}";
    }
    std::cout << "]}\n";
}

} // namespace lab

namespace {

int riskyFunction(int x) {
    if (x < 0) {
        throw std::invalid_argument("Negative value not allowed");
    }
    return x * 2;
}

class CustomRuntimeError : public std::runtime_error {
public:
    explicit CustomRuntimeError(const std::string& msg) : std::runtime_error(msg) {}
};

} // namespace

int main() {
    std::vector<lab::Case> cases;

    // Case 1: riskyFunction throws std::invalid_argument on negative input
    std::string msg1;
    int ok_val = riskyFunction(5);
    try {
        (void)riskyFunction(-1);
    } catch (const std::invalid_argument& e) {
        msg1 = e.what();
    }
    bool c1 = (ok_val == 10) && (msg1 == "Negative value not allowed");
    cases.push_back({"risky_function_invalid_argument", lab::jbool(true), lab::jbool(c1), c1});

    // Case 2: std::logic_error family (concrete base + 4 standard derived classes)
    bool logic_traits =
        std::is_base_of_v<std::exception, std::logic_error> &&
        std::is_base_of_v<std::logic_error, std::invalid_argument> &&
        std::is_base_of_v<std::logic_error, std::domain_error> &&
        std::is_base_of_v<std::logic_error, std::length_error> &&
        std::is_base_of_v<std::logic_error, std::out_of_range> &&
        !std::is_abstract_v<std::logic_error>;
    int logic_caught = 0;
    try { throw std::logic_error("L"); } catch (const std::logic_error& e) { if (std::string(e.what()) == "L") ++logic_caught; }
    try { throw std::invalid_argument("IA"); } catch (const std::logic_error& e) { if (std::string(e.what()) == "IA") ++logic_caught; }
    try { throw std::domain_error("DE"); } catch (const std::logic_error& e) { if (std::string(e.what()) == "DE") ++logic_caught; }
    try { throw std::length_error("LE"); } catch (const std::logic_error& e) { if (std::string(e.what()) == "LE") ++logic_caught; }
    try { throw std::out_of_range("OR"); } catch (const std::logic_error& e) { if (std::string(e.what()) == "OR") ++logic_caught; }
    bool c2 = logic_traits && (logic_caught == 5);
    cases.push_back({"logic_error_family", lab::jbool(true), lab::jbool(c2), c2});

    // Case 3: std::runtime_error family (concrete base + 3 standard derived classes)
    bool rt_traits =
        std::is_base_of_v<std::exception, std::runtime_error> &&
        std::is_base_of_v<std::runtime_error, std::range_error> &&
        std::is_base_of_v<std::runtime_error, std::overflow_error> &&
        std::is_base_of_v<std::runtime_error, std::underflow_error> &&
        !std::is_abstract_v<std::runtime_error>;
    int rt_caught = 0;
    try { throw std::runtime_error("R"); } catch (const std::runtime_error& e) { if (std::string(e.what()) == "R") ++rt_caught; }
    try { throw std::range_error("RE"); } catch (const std::runtime_error& e) { if (std::string(e.what()) == "RE") ++rt_caught; }
    try { throw std::overflow_error("OF"); } catch (const std::runtime_error& e) { if (std::string(e.what()) == "OF") ++rt_caught; }
    try { throw std::underflow_error("UF"); } catch (const std::runtime_error& e) { if (std::string(e.what()) == "UF") ++rt_caught; }
    bool c3 = rt_traits && (rt_caught == 4);
    cases.push_back({"runtime_error_family", lab::jbool(true), lab::jbool(c3), c3});

    // Case 4: Custom exception derived from std::runtime_error & base std::exception
    std::string custom_msg;
    bool base_ex_ok = false;
    try {
        throw std::exception();
    } catch (const std::exception& e) {
        base_ex_ok = (std::strlen(e.what()) > 0);
    }
    try {
        throw CustomRuntimeError("My custom exception occurred");
    } catch (const std::runtime_error& e) {
        custom_msg = e.what();
    }
    bool c4 = base_ex_ok && (custom_msg == "My custom exception occurred");
    cases.push_back({"custom_derived_runtime_error", lab::jbool(true), lab::jbool(c4), c4});

    lab::emit(cases);
    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-libs-stdexcept-lab/explore-cpp-libs-stdexcept.cpp

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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