8.1.simple Program To Implement Exception Handling

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

8.1.

Simple program to implement exception handling


program:
#include<iostream>
using namespace std;
class sampledivision
{
int a,b,c,d;
public:
void getdata()
{
cout<<"\na=";
cin>>a;
cout<<"\nb=";
cin>>b;
cout<<"\nc=";
cin>>c;
cout<<"\nd=";
cin>>d;
}

void divide()
{
float F;
try
{
if(c==0)
throw c;
F=a+(b*c)/c+d;
cout<<"\nResult="<<F<<endl;
}
catch(int c)
{
cout<<"\nCannot divide by zero"<<endl;
getdata();
divide();
}
}
}d;

int main()
{
d.getdata();
d.divide();
return(0);
}

output:
[4090@localhost ~]$ g++ ex81.cpp
[4090@localhost ~]$ ./a.out

a=1

b=1

c=0

d=1

Cannot divide by zero

a=1

b=1

c=1

d=1

Result=3
8.2.Program to implement stack using exception
handling
Program
#include<iostream>
using namespace std;
class stack{
int* arr;
int top,size;
public:
stack(){
top=-1;
size=-1;
}
int isfull(){
if(top==size-1)
return 1;
else
return 0;
}
int isempty(){
if(top==-1)
return 1;
else
return 0;
}
void display(){
string str="Stack underflow!";
int i;
if(isempty())
throw str;
else{
for(i=top;i>=0;i--)
cout<<arr[i]<<"\n";
}
}
void push(){
string str="Stack overflow!";
if(isfull())
throw str;
else{
cout<<"Enter the element to be inserted:";
cin>>arr[++top];
display();
}
}
void pop(){
string str="Stack underflow!";
if(isempty())
throw str;
else{
cout<<"Element being popped is "<<arr[top]<<"\n";
top--;
display();
}

}
void create(int n){
string str="Minimum stack size should be 3";
if(n<3)
throw str;
arr=new int[n];
top=-1;
size=n;
}
void resize(int newsize){
int *a=new int[newsize];
for(int i=0;i<=top;i++)
a[i]=arr[i];
delete arr;
arr=a;
size=newsize;
}

};

class myexception{
public:
void minsize(string str){
cout<<str<<endl;
}
void underflow(string str){
cout<<str<<endl;
}
int overflow(string str){
int newsize;
char ch;
cout<<str<<endl;
cout<<"Do you want to resize?(y/n):";
cin>>ch;
if(ch=='n')
return 0;
else{
cout<<"Enter new size:";
cin>>newsize;
return newsize;
}
}
};

int main(){
stack s;
myexception e;
int n,newsize;
int ch;
label:
try{
cout<<"Enter maximum no. of elements:";
cin>>n;
s.create(n);
}
catch(string str){
e.minsize(str);
goto label;
}
do{
cout<<"1.Push\n2.Pop\n3.Display\n4.Exit\nEnter choice:";
cin>>ch;
if(ch==1){
try{
s.push();
}
catch(string str){
newsize=e.overflow(str);
if(newsize!=0)
s.resize(newsize);
}
}
else if(ch==2){
try{
s.pop();
}
catch(string str){
e.underflow(str);
}
}
else if(ch==3){
try{
s.display();
}
catch(string str){
e.underflow(str);
}
}

}while(ch!=4);
}

Output:
[4090@localhost ~]$ g++ ex82.cpp
[4090@localhost ~]$ ./a.out
Enter maximum no. of elements:5
1.Push
2.Pop
3.Display
4.Exit
Enter choice:1
Enter the element to be inserted:1
1
1.Push
2.Pop
3.Display
4.Exit
Enter choice:1
Enter the element to be inserted:2
2
1
1.Push
2.Pop
3.Display
4.Exit
Enter choice:1
Enter the element to be inserted:3
3
2
1
1.Push
2.Pop
3.Display
4.Exit
Enter choice:1
Enter the element to be inserted:4
4
3
2
1
1.Push
2.Pop
3.Display
4.Exit
Enter choice:1
Enter the element to be inserted:5
5
4
3
2
1
1.Push
2.Pop
3.Display
4.Exit
Enter choice:1
Stack overflow!
Do you want to resize?(y/n):y
Enter new size:6
1.Push
2.Pop
3.Display
4.Exit
Enter choice:1
Enter the element to be inserted:6
6
5
4
3
2
1
1.Push
2.Pop
3.Display
4.Exit
Enter choice:1
Stack overflow!
Do you want to resize?(y/n):n
1.Push
2.Pop
3.Display
4.Exit
Enter choice:3
6
5
4
3
2
1
1.Push
2.Pop
3.Display
4.Exit
Enter choice:2
Element being popped is 6
5
4
3
2
1
1.Push
2.Pop
3.Display
4.Exit
Enter choice:2
Element being popped is 5
4
3
2
1
1.Push
2.Pop
3.Display
4.Exit
Enter choice:2
Element being popped is 4
3
2
1
1.Push
2.Pop
3.Display
4.Exit
Enter choice:2
Element being popped is 3
2
1
1.Push
2.Pop
3.Display
4.Exit
Enter choice:2
Element being popped is 2
1
1.Push
2.Pop
3.Display
4.Exit
Enter choice:2
Element being popped is 1
Stack underflow!
1.Push
2.Pop
3.Display
4.Exit
Enter choice:3
Stack underflow!
1.Push
2.Pop
3.Display
4.Exit
Enter choice:4
[4090@localhost ~]$

You might also like