0% found this document useful (0 votes)
46 views4 pages

Stack Program Using Exception Handling Program

This C++ program implements a stack using an array and exception handling. It defines FULL and EMPTY classes to throw exceptions for when the stack is full or empty. The stack class contains push(), pop(), and display() methods. The main() function creates a stack, displays a menu, and allows the user to push, pop, or display the stack. It catches any FULL or EMPTY exceptions thrown and prints the appropriate error message.

Uploaded by

John Berkmans
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views4 pages

Stack Program Using Exception Handling Program

This C++ program implements a stack using an array and exception handling. It defines FULL and EMPTY classes to throw exceptions for when the stack is full or empty. The stack class contains push(), pop(), and display() methods. The main() function creates a stack, displays a menu, and allows the user to push, pop, or display the stack. It catches any FULL or EMPTY exceptions thrown and prints the appropriate error message.

Uploaded by

John Berkmans
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

STACK PROGRAM USING EXCEPTION HANDLING

Program
#include<iostream>
#include<iomanip>
#include<process>
using namespace std;
class stack
{
private:
int *s;
int max;
int top;
public:
class FULL{}; //for exception handling
class EMPTY{}; //for exception handling
stack(int);
void push(int);
int pop(void);
void display(void);
};
stack::stack(int m)
{
s=new int[m];
top=-1;
max=m;
}
void stack::push(int item)
{
if(top<max-1)
s[++top]=item;
else
throw FULL(); //FULL object is thrown
}
int stack::pop(void)
{
if(top>=0)
return s[top];
else
throw EMPTY(); //EMPTY object is thrown
}
void stack::display()
{
if(top>=0)
for(int i=top; i>=0;i)
cout<<i<<endl;
else
throw EMPTY();
}
int main()
{
int item, size;
int opt;
cout<<\nEnter the size of stack;
cin>>size;
stack s1(size);
cout<<\nStack with Exception Handling;
cout<<\n\n\tMENU\n1.PUSH\n2.POP\n3.SHOW STACK\n4.EXIT;
cout<<\nEnter your choice;
cin>>opt;
do
{
switch(ch)
{
case 1:
{
cout<<\nEnter the item to push;
cin>>item;
try
{
s1.push(item);
}
catch(stack::FULL) //FULL object is caught
{
cout<<Stack Overflow<<endl;
}
break;
}
case 2:
{
try
{
cout<<Poped Item is<<s1.pop()<<endl;
}
catch(stack::EMPTY) //EMPTY object caught
{
cout<<Stack Empty<<endl;
}
break;
}
case 3:
{
cout<<The Content Of Stack is<<endl;
try
{
s1.display();
}
catch(stack::EMPTY)
{
cout<<Stack Empty<<endl;
}
break;
}
cout<<Enter your choice<<endl;
cin>>opt;
}
}while(opt<=3);
return 0;
}

You might also like