为什么这个基本的 Try-Catch 无法捕获

问题描述

我正在学习 C++ 中的 try-catch 构造,并且我有以下示例似乎无法执行任一捕获中的代码。在过去的几个小时里,我一直在努力寻找错误/问题,但运气不佳。

我想知道我的机器上的 g++ 是否有问题——我使用的是 mingw 的 g++ 和 Windows 10。

#include <iostream>
#include <stdexcept>

int main(){

    try {
        std::cout << "Start of Try-Catch\n";
        int a = 13;
        int b = 0;
        int p = a/b;
        std::cout << "printing p: " << p << std::endl;
        p = 43;

        std::cout << "Passed the div by zero issue\n";
    } catch (std::runtime_error& e){
        std::cout << "runtime error: " << e.what() << '\n';
        return 2;
    } catch (std::exception& e){
        std::cout << "other error: " << e.what() << '\n'; 
        return 3;
    } catch (...) {
        std::cout << "final catch\n";
        return 4;
    }
    std::cout << "end of program\n";
    return 0;
}

相反,这是我编译和运行时发生的情况:

C:\Users\...\Part 1>g++ cp_bug.cpp -std=c++17

C:\Users\...\Part 1>a.exe
Start of Try-Catch

C:\Users\...\Part 1>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)