#include<iostream>
using namespace std;
#include<string>
#include <stdexcept>//使用标准的异常库
//out of range
class Teacher
{
public:
Teacher(int age)
{
if (age>100)
{
string s = "年龄太大";
throw out_of_range(s);
//继承自logic_error-exception
}
this->age = age;
}
private:
int age;
};
int main(void)
{
try
{
Teacher t1(200);
}
catch (out_of_range e)
{
cout << e.what() << endl;
}
exception e;
system("pause");
return 0;
}
/*
*年龄太大
请按任意键继续. . .
*-------------------------------------------------
* logic_error和runtime_error两个类及派生类,都有一个接受
* const string&类型的构造函数。在构造异常对象的时候需要将具体
* 的错误信息传递给该函数。如果调用该对象的what()函数,就可以
* 得到构造时提供的错误信息。
* exception类中提供了一个成员函数what,用于返回错误信息。
* _EXCEPTION_INLINE virtual const char * __CLR_OR_THIS_CALL what() const;
*
*
*/
009从标准模板类定义老师类_判断老师年龄是否超过100岁
猜你喜欢
转载自blog.csdn.net/baixiaolong1993/article/details/89502276
今日推荐
周排行