Why did my exception fall on some configurations but not on others?

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; //... errmsg << "Error occured."; throw CException( 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!

+4
source share
1 answer

I found out that the problem was caused by using the Fortran95 compiler. It is used as a linker when creating a program on a Sun machine; on other machines, g ++ is used. I have no idea what the problem is, but I think I'm just switching to g ++ on Sun machines.

+1
source

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


All Articles