Strange problems with C ++ exceptions with mingw

I was having strange exception problems using mingw and managed to shorten it 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, and the second is not, so I get a beautiful window with a Windows error informing me that my application has stopped working :-( Full command line output:

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

This application requested Runtime to complete it in an Unusual way. Contact customer support information.

The same thing happens with other exceptions (e.g. std :: runtime_error).

Am I doing something wrong, or is the problem elsewhere?

System Information: Windows 7 x64, the latest version of mingw32 (was installed yesterday using mingw-get from mingw.org).

Thank you in advance. Michal

+6
source share
3 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 file:

  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) 

Exit

 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

  • library incompatibility
  • environmental differences
  • error (?) in your version of MingW
+2
source

No, I do not think that you are doing something wrong, it is pretty standard and works well under Linux.

I suggest raising a query with the MinGW people. Even if this is not a mistake, they should be able to tell you what is happening.

+1
source

I also have 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) 

Exit:

 $ 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 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 that it provides. In this case, I did this with foo.c :

 int foo() { return 0; } 
0
source

Source: https://habr.com/ru/post/899284/


All Articles