我遇到了使用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错误框通知我我的应用程序已停止工作:-( 完整的命令行输出是:
捕获异常:a&lt; 0 ..继续无论如何
文件错误 - &gt;抛出异常此应用程序已请求运行时将其终止 不寻常的方式请联系应用程序的支持团队获取更多信息 信息。
同样的情况也发生在其他异常中(例如std :: runtime_error)。
我做错了什么,或者是其他地方的问题?
系统信息:Windows 7 x64,最新的mingw32(昨天使用mingw-get从mingw.org重新安装)。
提前多多谢谢 米甲
答案 0 :(得分:2)
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'
所以这是指向
方向的软证据答案 1 :(得分:1)
不,我不认为你在那里做错了什么,这是非常标准的,在Linux下运行得很好。
我建议与MinGW人员raising a query。即使它不是一个错误,它们也应该能够告诉你发生了什么。
答案 2 :(得分:0)
我也遇到了这个问题(6年后),除了我在Windows 7上找到了MSYS2,cmake / ninja / mingw32:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(FailedExceptions )
add_executable(FailedExceptions main.cpp foo.c)
输出:
$ cmake .. -GNinja
-- The C compiler identification is GNU 7.2.0
-- The CXX compiler identification is GNU 7.2.0
-- Check for working C compiler: C:/msys64/mingw32/bin/cc.exe -- works
-- Check for working CXX compiler: C:/msys64/mingw32/bin/c++.exe -- works
-- Configuring done
-- Generating done
-- Build files have been written to: C:/msys64/home/sferguson/src/vis/build
$ ninja -v
[1/3] C:\msys64\mingw32\bin\cc.exe -MD -MT CMakeFiles/FailedExceptions.dir/PortDescription.c.obj -MF CMakeFiles\FailedExceptions.dir\PortDescription.c.obj.d -o CMakeFiles/FailedExceptions.dir/PortDescription.c.obj -c ../PortDescription.c
[2/3] C:\msys64\mingw32\bin\c++.exe -MD -MT CMakeFiles/FailedExceptions.dir/main.cpp.obj -MF CMakeFiles\FailedExceptions.dir\main.cpp.obj.d -o CMakeFiles/FailedExceptions.dir/main.cpp.obj -c ../main.cpp
[3/3] cmd.exe /C "cd . && C:\msys64\mingw32\bin\c++.exe CMakeFiles/FailedExceptions.dir/main.cpp.obj CMakeFiles/FailedExceptions.dir/PortDescription.c.obj -o FailedExceptions.exe -Wl,--major-image-version,0,--minor-image-version,0 -lgcc_eh -lgcc_eh -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
$ ./FailedExceptions.exe
Exception caught: a < 0: iostream error
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.
唯一的问题是我还需要链接一些(任何)c文件,即使我不使用它提供的任何内容。在这种情况下,我使用foo.c
:
int foo() { return 0; }