You are on page 1of 3

Exception Handling:

#include<iostream.h> #include<stdlib.h> #define max 5 class stack { private: int a[max]; int top; public: stack !; void push int item!; void pop !; void displa" !; #; stack::stack ! { top$%&; # void stack::push int item! { int fla'; tr" { fla'$(; if top$$max%&! { fla'$&; thro) &; # catch int! { cout<<*catch on exception+stackis full ,n*; # if fla'$$(! { top--; a[top]$item; # # void stack::pop !

{ int fla'; tr" { fla'$(; if top$$%&! { fla'$&; thro) &; # # catch int! { cout<<*catch an exception+stack is empt" ,n*; # if fla'$$(! { cout<<*,n .eleted element is:*<<a[top]; top$top%&; # # void stack::displa" ! { int fla'$(; tr" { if top$$%&! { fla'$&; thro) &; # # catch int! { cout<<*cau'ht an exception stack is empt" ,n*; # if fla'$$(! { cout<<*elementsn in the stack,n*; for int i$top;i>$(;i--! cout<<*a[*<<i/<<*]$*<<a[i]<<*,n*; # #

int main ! { stack s; int item0ch; cout<<*implementation of stack ,n*; )hile ch<5! { cout<<*,n &.push ,n 1.pop ,n 2.displa" ,n 3.exit ,n enter the choice ,n*; cin>>ch; s)itch ch! { case &: cout<<*,n enter the element to puush ,n*; cin>>item; s.push item!; break; case 1: s.pop !; break; case 2: s.displa" !; break; case 3: exit (!; # # return (; #

You might also like