目录
- 最好不要再函数中包括输入输出语句,不符合类的重用的特征
1.异常处理流程
(1)try块中的语句再执行的过程中没有异常抛出,跳过catch
(2)try块中的语句再执行的过程中有异常抛出,捕获catch匹配异常类型,执行异常处理代码
(3)try块中的语句再执行的过程中有异常抛出,但是没有匹配捕获,造成程序终止
2.自定义异常类的使用
Stack s1(2);
char ch;
try{
s1.push('a');
s1.push('b');
s1.push('c');
s1.push('d');
}catch(Stack::Full){
cout<<"Stack Full!!!"<<endl;
}
cout<<s1.isFull()<<endl;
cout<<s1.getTop()<<endl;
try{
ch = s1.pop();
cout<<ch<<endl;
ch = s1.pop();
cout<<ch<<endl;
ch = s1.pop();
cout<<ch<<endl;
ch = s1.pop();
cout<<ch<<endl;
}catch(Stack::Empty){
cout<<"Stack Empty!!!"<<endl;
}
class Empty{};
class Full{};