Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strange problems with C++ exceptions with mingw

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

like image 352
Michal Kaut Avatar asked Oct 14 '11 09:10

Michal Kaut


2 Answers

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
like image 69
sehe Avatar answered Sep 19 '22 00:09

sehe


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.

like image 45
paxdiablo Avatar answered Sep 18 '22 00:09

paxdiablo