C++ 的奇怪问题mingw 的例外情况

发布于 2024-12-10 04:01:47 字数 1346 浏览 1 评论 0原文

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

This 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

避讳 2024-12-17 04:01:47

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 版本中的

FWIW, on XP SP3 with MingW:

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)

Results in 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)

Output

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

So this is soft evidence pointing in the direction of

  • library incompatibility
  • environmental differences
  • bug (?) in your version of MingW
隱形的亼 2024-12-17 04:01:47

不,我不认为你在那里做错了什么,这是非常标准的并且在 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.

人心善变 2024-12-17 04:01:47

我也遇到了这个问题(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; }

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:

cmake_minimum_required(VERSION 3.6)
project(FailedExceptions )
add_executable(FailedExceptions  main.cpp foo.c)

Output:

$ 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.

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:

int foo() { return 0; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文