gpt4 book ai didi

c++ - mingw 的 C++ 异常的奇怪问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:47 25 4
gpt4 key购买 nike

我在使用 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;
}

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

Exception caught: a < 0 .. continue anyway
file error -> throwing exception

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

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

是我做错了什么,还是其他地方出了问题?

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

提前致谢。
迈克尔

最佳答案

FWIW,在带有 MingW 的 XP SP3 上:

Using built-in specs.
Target: mingw32
Configured with: ../gcc-4.4.0/configure --prefix=/mingw --build=mingw32 --enable-languages=c,ada,c++,fortran,objc,obj-c++ --disable-nls --disable-win32-registry --disable-werror --enable-threads --disable-symvers --enable-cxx-flags='-fno-function-sections -fno-data-sections' --enable-fully-dynamic-string --enable-libgomp --enable-version-specific-runtime-libs --enable-sjlj-exceptions --with-pkgversion='TDM-1 mingw32' --with-bugurl=http://www.tdragon.net/recentgcc/bugs.php
Thread model: win32
gcc version 4.4.0 (TDM-1 mingw32)

a.exe 中的结果:

    ntdll.dll => /cygdrive/c/WINDOWS/system32/ntdll.dll (0x7c900000)
kernel32.dll => /cygdrive/c/WINDOWS/system32/kernel32.dll (0x7c800000)
msvcrt.dll => /cygdrive/c/WINDOWS/system32/msvcrt.dll (0x77c10000)

输出

Exception caught: a < 0 .. continue anyway
file error -> throwing exception
Exception caught: could not open input file 'file-that-does-not-exist'

所以这是指向方向的软证据

  • 库不兼容
  • 环境差异
  • 您的 MingW 版本中的错误 (?)

关于c++ - mingw 的 C++ 异常的奇怪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7765599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com