Professional Documents
Culture Documents
CSC-241
ASSIGNMENT # 04
LAB: EXCEPTION HANDLING
FA20-BEE-040
Registration
Number
Class 3B (BEE)
Lab Assessment
#include<iostream>
#include<conio.h>
int main(){
cin>>FirstValue>>SecondValue;
int ConditionVariable;
try{
if (ConditionVariable == 0){
cout<<"Subtraction (FirstValue-SecondValue)="<<FirstValue-
SecondValue<<"\n";
else{
throw(ConditionVariable);
catch(int thrownValue){
cout<<endl<<endl;
system("pause");
}
Q: Create a program which will through exception by defining multiple catch statements.
#include<iostream>
#include<conio.h>
try{
value;
if (value==0)
throw value;
}
catch(char ThrownValue){
catch(int ThrownValue){
value;
catch(double ThrownValue){
int main( ){
Function( 5);
Function(-1);
cout<<endl<<endl;
system("pause");
}
Q: Create a program having two values with throw mechanisms, which will throw
exception if value of first variable is 0.
#include<iostream>
#include<conio.h>
try{
if(FirstValue==0)
throw FirstValue;
else
cout<<"Subtraction ="<<FirstValue-SecondValue;
catch(int){
}
cout<<"*** End of Subtract function***\n \n";
int main(){
try{
Subtract(8, 5);
Subtract(0, 8);
catch(int){
cout<<endl<<endl;
system("pause");
}
Q: Practice the above mentioned examples
#include<iostream>
#include<conio.h>
int main(){
int ConditionVariable;
cin>>ConditionVariable;
try{
else{
throw(ConditionVariable);
catch(int thrownValue){
cout<<endl<<endl; system("pause");
}
Q: Create a class which only works for absolute numbers, if it encounters any negative
occurrence, then it throw an exception to its handler and display errors.
#include<iostream>
#include<conio.h>
class A{
public:
int ConditionVariable;
class AnError
{ };
void getdata(){
cout<<"ConditionVariable ";
cin>>ConditionVariable;
void Func(){
if(ConditionVariable < 0)
throw AnError();
};
int main(){
try{
A obj1;
obj1.getdata();
obj1.Func();
catch(A::AnError){
cout<<"error is ";
return 0;
Q: The queue is another data structure. A physical analogy for a queue is a line at a bank.
When you go to the bank, customers go to the rear (end) of the line and customers who are
serviced come out of the line from the front of the line.
The main property of a queue is that objects go on the rear and come off of the front of the
queue.
Make-Queue
Create a new, empty queue object.
Empty
Reports whether queue is empty or not.
Enter(or Insert)
Places an object at the rear of the queue
Delete (or Remove)
Removes an object from the front of the queue and produces that object.
Write a program to create a queue class and do queue operations with exception
handling.
#include<iostream>
class queue{
public:
int size;
int *a;
queue(int sz){
size=sz;
a=new int[sz];
front =-1;
rear=-1;
void insert(int c)
{
if(rear==size-1)
throw rear;
else
a[++rear]=c;
void remove()
if(front==rear)
throw 'u';
else
cout<<"\n"<<a[++front]<<" removed\n";
void display();
};
void queue::display()
cout<<"\nQueue:\n";
cout<<a[i]<<" ";
}
int main()
int sz,x,ch;
cin>>sz;
queue s(sz);
try
do
cin>>ch;
switch(ch)
cin >>x;
s.insert(x);
break;
case 2:s.remove();
break;
case 3:s.display();
break;
case 4:return 0;
break;
default:
}while(1);
catch(int a)
if(a==s.size-1)
cout<<"\nOverflow\n";
catch(char c)
if(c=='u')
cout<<"\nUnderflow.";
return 0;