自己编写异常类的时候出现的错误
[Error] looser throw specifier for 'virtual MyOutOfRangException:MyOutOfRangExceptionO
需要在出现此类错误的函数后面加上throw() 即可;
#include <iostream>
#include <string>
using namespace std;
//Ctrl+鼠标左键即可跳转;
class MyOutOfRangeException:public exception
{
public:
MyOutOfRangeException(string errorInfo)
{
this->m_ErrorInfo=errorInfo;
}
virtual ~MyOutOfRangeException() throw()
{
}
virtual const char* what() const throw()
{
//c_str()可以实现string转char *
return this->m_ErrorInfo.c_str();
}
string m_ErrorInfo;
};
class Person
{
public:
Person(int age,string str)
{
this->age=age;
this->name=str;
if(age<0 || age >200)
{
throw MyOutOfRangeException(string("我自己的年龄越界异常了"));
}
}
private:
int age;
string name;
};
void test01()
{
try
{
Person p(300,"张三");
}
catch(MyOutOfRangeException &e)
{
cout << e.what() << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}