【发布时间】:2016-05-20 13:08:37
【问题描述】:
我刚开始在 C++ 中使用异常,但每次调用异常时,我的程序都会终止:
在抛出 'MyException1' 的实例后调用终止
what(): Hier koennte Ihre Werbung stehen
我不知道为什么。 请注意,我必须使用 C++03,因为我正在为 QNX 系统编程。
我的异常类:
using namespace std;
#include <exception>
class Exception : public exception
{
public:
Exception(const char* msg): msg_((char*)msg) {};
virtual const char* what() const throw()
{
return msg_;
}
virtual ~Exception() throw(){};
private:
char* msg_;
};
class MyException1 : public Exception
{
public:
MyException1(const char* message):Exception(message){}
};
抛出和捕获异常的类:
using namespace std;
#include "ExceptionUseExample.h"
#include <iostream>
void ExceptionUseExample::doSomethingElse() throw()
{
throw MyException1("Hier koennte Ihre Werbung stehen");
}
void ExceptionUseExample::doSomething()
{
try
{
doSomethingElse();
}
catch (MyException1& e)
{
cerr << "catched MyException1: " << e.what() << endl;
}
}
【问题讨论】: