C++ 的奇怪问题mingw 的例外情况
我在使用 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 < 0 .. 仍然继续
文件错误->抛出异常此应用程序已请求运行时终止它 不寻常的方式。请联系应用程序的支持团队了解更多信息 信息。
其他异常也会发生同样的情况(例如 std::runtime_error)。
是我做错了什么,还是其他地方有问题?
系统信息:Windows 7 x64,最新的 mingw32(昨天使用 mingw.org 的 mingw-get 重新安装)。
预先非常感谢。
米哈尔
I have encountered a strange problems with exceptions using mingw and managed to cut it down to the following example:
#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;
}
The first exception is caught, but the second one does not, so I get the nice Windows error-box informing me that my application has stopped working :-(
The full command-line output is:
Exception caught: a < 0 .. continue anyway
file error -> throwing exceptionThis application has requested the Runtime to terminate it in an
unusual way. Please contact the application's support team for more
information.
The same happen also with other exceptions (like std::runtime_error).
Am I doing something wrong, or is the problem somewhere else?
System info: Windows 7 x64, latest mingw32 (re-installed yesterday using mingw-get from mingw.org).
Thanks a lot in advance.
Michal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
FWIW,在带有 MingW 的 XP SP3 上:
a.exe 中的结果:
输出
因此,这是指向
FWIW, on XP SP3 with MingW:
Results in a.exe:
Output
So this is soft evidence pointing in the direction of
不,我不认为你在那里做错了什么,这是非常标准的并且在 Linux 下工作得很好。
我建议向 MinGW 人员提出疑问。即使它不是错误,他们也应该能够告诉您发生了什么。
No, I don't think you're doing anything wrong there, this is pretty standard and works quite well under Linux.
I would suggest raising a query with the MinGW people. Even if it's not a bug, they should be able to tell you what's going on.
我也遇到了这个问题(6年后),除了我的问题是在Windows 7上使用MSYS2、cmake/ninja/mingw32找到的:
CMakeLists.txt:
输出:
唯一的问题是我还需要链接一些(任何) c 文件,即使我不使用它提供的任何内容。在本例中,我使用 foo.c 来完成:
I'm also having a problem with this (6 years later), except mine is found with MSYS2, cmake/ninja/mingw32 on windows 7:
CMakeLists.txt:
Output:
The only catch is that I also need to link some (any) c file, even if I don't use anything it provides. In this case I did it with
foo.c
: