// ============================================================================ // p1c07_05 — 7.2.4 异常接口声明 // 来源:川大 C++/LinuxC wiki「02.Qt方向」 // 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用 // // --- 原文片段 [pageId:blockIdx] --- 标注出处。 // 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。 // ============================================================================ #include using namespace std; // --- 原文片段 [58954474:4] 7.2.4 异常接口声明 --- //可抛出所有类型异常 void TestFunction01(){ throw 10; } // 【C++17 修正】原文为 throw(int,char,char*) 动态异常规范——C++17 起非法 // (C++11 弃用、C++17 移除)。动态规范无法在现代 C++ 中精确表达「只抛这些类型」, // 等价改写为 noexcept(false)(表示可能抛异常)。原文语义见 tools/demo_versions/。 void TestFunction02() noexcept(false){ string exception = "error!"; throw exception; } // 【C++17 修正】原文为 throw()(不抛任何异常)→ C++17 等价于 noexcept。 void TestFunction03() noexcept { //throw 10; // noexcept 函数抛异常会调用 std::terminate } int main(){ try{ //TestFunction01(); //TestFunction02(); //TestFunction03(); } catch (...){ cout << "捕获异常!" << endl; } return EXIT_SUCCESS; } // --- main ---