#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdexcept>
class maker {
public:
maker(int age) {
if (age < 0 or age>150) {
throw std::out_of_range("年龄不在范围内\n");
}
else {
this->age = age;
}
}
int age;
};
void test() {
try {
maker m(200);
}
catch (std::out_of_range& ex) {
std::cout << ex.what() << '\n';
}
}
int main(){
test();
return 0;
}
异常的基类与之对应的继承关系
![](/i/l/?n=24&i=blog/3368337/202401/3368337-20240115185745136-1827062328.png)