#include using namespace std; const int MAX =3; class Stack { private: int st[MAX]; int top; public: class pushException {}; class popException {}; Stack() { top = -1; } void push (int var) { if (top >= MAX-1) throw pushException(); st[++top] = var ; } int pop() { if (top <0) throw popException(); return st[top--]; } }; //////////////////////////////////////////////////////////////// int main() { Stack s1; string st; try { s1.push(11); s1.push(22); s1.push(33); //s1.push(44); //oops: stack full cout << "1: " << s1.pop() << endl; cout << "2: " << s1.pop() << endl; cout << "3: " << s1.pop() << endl; cout << "4: " << s1.pop() << endl; //oops: stack empty } catch(Stack::pushException) { cout << "Exception thrown: The stack is full "; } catch(Stack::popException) { cout << "Exception thrown: The stack is empty "; } cout << "Arrive here after catch (or normal exit)" << endl; cin >> st; return 0; }