#include <stdexcept>
using namespace std;
class MyOutOfRangException :public exception
{
public:
MyOutOfRangException(string errorInfo) {
this->m_ErrorInfo = errorInfo;
}
virtual char const* what() const
{
//string转char *
return this->m_ErrorInfo.c_str();
}
virtual ~MyOutOfRangException() {
}
string m_ErrorInfo;
};
class Person
{
public:
Person(string name, int age) {
this->m_Name = name;
if (age < 0 || age>200)
{
//throw MyOutOfRangException(string("我自的异常类"));
throw MyOutOfRangException("我自的异常类");
}
}
string m_Name;
int m_Age;
};
void test01()
{
Person p2("小王", 3000);
}
int main()
{
try
{
test01();
}
catch (MyOutOfRangException& e)
{
cout << e.what() << endl;
}
return 0;
}
原文:https://www.cnblogs.com/lodger47/p/14705860.html