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
source share