与mingw的C++异常的奇怪问题

Mic*_*aut 6 c++ exception-handling mingw32

我遇到了使用mingw的异常的奇怪问题,并设法将其剪切为以下示例:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

void test(int a) {
    if (a < 0) {
        throw std::ios_base::failure("a < 0");
    }
}
void test_file(std::string const & fName)
{
    std::ifstream inF(fName.c_str(), std::fstream::in);
    if (!inF) {
        cout << "file error -> throwing exception" << endl;
        throw ios_base::failure("could not open input file '" + fName + "'");
    }
}

int main()
{
    try { test(-5); }
    catch(std::exception& e) {
        cerr << "Exception caught: " << e.what() << " .. continue anyway" <<endl;
    }

    try { test_file("file-that-does-not-exist"); }
    catch(std::exception& e) {
        cerr << "Exception caught: " << e.what() << endl;
        exit(EXIT_FAILURE);
    }
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

第一个异常被捕获,但第二个没有,所以我得到了漂亮的Windows错误框通知我我的应用程序已停止工作:-(完整的命令行输出是:

捕获异常:<0 ..仍然继续
文件错误 - >抛出异常

此应用程序已请求Runtime以不寻常的方式终止它.有关更多信息,请联系应用程序的支持团队.

其他异常(如std :: runtime_error)也会发生同样的情况.

我做错了什么,或者是其他地方的问题?

系统信息:Windows 7 x64,最新的mingw32(昨天使用mingw-get从mingw.org重新安装).

非常感谢提前.
米哈尔

pax*_*blo 1

不,我不认为你在那里做错了什么,这是非常标准的并且在 Linux 下工作得很好。

我建议向 MinGW 人员提出疑问。即使这不是错误,他们也应该能够告诉您发生了什么。