#include<stdio.h>
#include
using namespace std;
class Stack
{
int sta[1];
int top;
public:
Stack(){top = 0;}
class Full{}; //这里写成类
class Empty{};
void push(int a){
top++;
if(top > 1)throw Full();
sta[top] = a;
}
void pop(){
top–;
if(top == -1)throw Empty();
sta[top] = 0;
}
};
int main()
{
Stack s;
try {
s.push(1);
s.pop();
s.push(2);
s.push(3);
s.pop();
s.pop();
}
catch(Stack::Empty){
printf(“栈空无法出栈!”);
}
catch(Stack::Full){
printf(“栈满无法入栈!”);
}
/*
执行了上面的catch,程序就退出了
*/
try{
s.pop();
s.pop();
}
catch(Stack::Empty){
printf(“栈空无法入栈!”);
}
catch(Stack::Full){
printf(“栈满无法入栈!”);
}
return 0;
}
c++中的try catch
最新推荐文章于 2024-06-22 09:09:30 发布