私はC++を勉強しています。私自身の例外を作成してLinuxに投げ込もうとしています。C++でカスタム例外を作成する
実装をテストするための小さなテストプロジェクトを作成しましたが、以下は例外クラスのヘッダーファイルです。
class TestClass : public std::runtime_error
{
public:
TestClass(char const* const message) throw();
virtual char const* what() const throw();
};
例外クラスのソースファイルが私のメインアプリで
using namespace std;
TestClass::TestClass(char const* const message) throw()
: std::runtime_error(message)
{
}
char const * TestClass::what() const throw()
{
return exception::what();
}
で、私は例外をスローし、次のようにのtry/catchでそれをキャッチする関数を呼び出しています:
void runAFunctionAndthrow();
/*
*
*/
int main(int argc, char** argv) {
try
{
cout << "About to call function" << endl;
runAFunctionAndthrow();
}
catch (TestClass ex)
{
cout << "Exception Caught: " << ex.what() << endl;
}
return 0;
}
void runAFunctionAndthrow()
{
cout << "going to run now. oh dear I need to throw an exception" << endl;
stringstream logstream;
logstream << "This is my exception error. :(";
throw TestClass(logstream.str().c_str());
}
私は次のような出力を得るために期待してい実行します。
を私が取得しています何の代わりに10About to call function
Going to run now. oh dear I need to throw an exception
Exception Caught: This is my exception error. :(
ではなく、私の実際の例外メッセージのSTD ::例外は、「これは私の例外エラーです」と言い、最後の行
About to call function
going to run now. oh dear I need to throw an exception
Exception Caught: std::exception
お知らせです。
これはなぜですか、Windowsでは問題なく動作しますが、Linuxではこれが行われます。
さまざまな投稿で見たことから、私が行ったことは正しいので、何が欠けているのでしょうか。
'what()'では、おそらく 'return runtime_error :: what();'を意味しています - 再実装を完全に省略することになります(ここでの基本クラスの振る舞いは既にあなたには良いことです)。 –