I have a program that throws an exception that falls on some configurations (Suse Linux, g ++ version 4.4.1), as expected, but obviously does not fall on another one, here: SunOS 5.10, g ++ version 3.3.2. The following is the implementation of my exception class:
CException.hpp:
#ifndef _CEXCEPTION_HPP #define _CEXCEPTION_HPP #include <string> #include <sstream> #include <exception> #include <stdlib.h> #include <iostream> class CException : public std::exception { public: CException(); CException(const std::string& error_msg); CException( const std::stringstream& error_msg ); CException( const std::ostringstream& error_msg ); virtual ~CException() throw(); const char* what() const throw(); static void myTerminate() { std::cout << "unhandled CException" << std::endl; exit(1); }; private: std::string m_error_msg; };
CException.cpp:
#include "CException.hpp" #include <string> #include <sstream> CException::CException() { std::set_terminate(myTerminate); m_error_msg = "default exception"; } CException::CException(const std::string& error_msg) { std::set_terminate(myTerminate); m_error_msg = error_msg; } CException::CException(const std::stringstream& error_msg) { std::set_terminate(myTerminate); m_error_msg = error_msg.str(); } CException::CException(const std::ostringstream& error_msg) { std::set_terminate(myTerminate); m_error_msg = error_msg.str(); } CException::~CException() throw() { } const char* CException::what() const throw() { return m_error_msg.c_str(); } #endif /* _CEXCEPTION_HPP */
Unfortunately, I was unable to create a simple program to reproduce the problem, but I will try to describe the code. An exception is thrown in the foo() function in some Auxiliary.cpp file:
std::ostringstream errmsg;
The foo() function is used in the main program:
#include Auxiliary.hpp //... int main( int argc, char** argv ) { try { //... foo(); } catch ( CException e ) { std::cout << "Caught CException" << std::endl; std::cout << "This is the error: " << e.what( ) << std::endl; } catch ( std::exception& e ) { std::cout << "std exception: " << e.what( ) << std::endl; } catch ( ... ) { std::cout << "unknown exception: " << std::endl; }
I see that the exception does not get caught, because the program exits with an unhandled CException seal, which is defined by myTerminate() .
I tried the -fexceptions option for the GNU compiler without success. The compiler options are virtually the same for both systems.
At the moment, I just canβt understand what the problem is. Any ideas are welcome. Thanks!