[英]Why is my exception caught on some configurations and on others is not?
我有一個程序拋出了一些異常,這些異常在某些配置上被捕獲(Suse Linux,g ++版本4.4.1),但顯然沒有被另一個配置,在這里:SunOS 5.10,g ++版本3.3.2。 以下是我的異常類的實現:
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 */
不幸的是,我無法創建一個簡單的程序來重現這個問題,但我會嘗試概述代碼。 在某些文件Auxiliary.cpp
中的函數foo()
中拋出異常:
std::ostringstream errmsg;
//...
errmsg << "Error occured.";
throw CException( errmsg );
函數foo()
用於主程序:
#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;
}
我可以看到,當程序以打印unhandled CException
的myTerminate()
由myTerminate()
定義myTerminate()
退出時,不會捕獲該異常。
我已經嘗試了GNU編譯器的-fexceptions
選項而沒有成功。 兩個系統上的編譯器選項實際上是相同的。
目前我無法弄清問題是什么。 任何想法都表示贊賞。 謝謝!
我發現問題是由使用Fortran95編譯器引起的。 它在Sun機器上構建程序時用作鏈接器,在其他機器上使用g ++。 我不知道究竟是什么問題,但我想我也會在Sun機器上切換到g ++。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.