本人初学C++,各位大神帮忙看下下面的这段C++程序代码,编译提示2个错误,小弟实在找不出啊~~

2025-03-02 01:25:15
推荐回答(2个)
回答1:

把类名改了,比如改成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;
}

回答2:

#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;
}