把类名改了,比如改成exception1,exception这个名字系统定义过
#include
#include
using namespace std;
class exception1 //自定义的异常类,类名改了
{
private:
string message; //错误提示信息
public:
exception1() //构造函数
{
message="自定义的异常类";
}
string getmessage()
{
return message;
}
};
int main()
{
try
{
int b=0;
if(b==0)
throw exception1();//抛出自定义类异常
}
catch(exception1 e)
{
cout<
return 0;
}
#include
#include
using namespace std;
class XXX:exception //自定义的异常类要继承于exception
{
private:
string message; //错误提示信息
public:
XXX:exception() //构造函数
{
message="自定义的异常类";
}
string getmessage()
{
return message;
}
};
int main()
{
try
{
int b=0;
if(b==0)
throw XXX:exception();//抛出自定义类异常的方式是这样
}
catch(XXX:exception e)//同上理
{
cout<
return 0;
}