You are on page 1of 13

OBJECT ORIENTED PROGRAMMING

CSC-241
ASSIGNMENT # 04
LAB: EXCEPTION HANDLING

Name BILAL SALEEM AHMED

FA20-BEE-040
Registration
Number

Class 3B (BEE)

Instructor’s Name Ma’am Asma Jadoon

Lab Assessment

Post Lab Total


Pre-Lab In-Lab
Data Presentation Data Analysis Writing Style
Q: Create a program which will through exception when first variable is smaller than
second, if it is not smaller then subtract two values.

#include<iostream>

#include<conio.h>

using namespace std;

int main(){

int FirstValue, SecondValue;

cout<<"\n Enter values of FirstValue and SecondValue\n";

cin>>FirstValue>>SecondValue;

int ConditionVariable;

ConditionVariable=FirstValue >SecondValue? 0 :1;

try{

if (ConditionVariable == 0){

cout<<"Subtraction (FirstValue-SecondValue)="<<FirstValue-
SecondValue<<"\n";

else{

throw(ConditionVariable);

catch(int thrownValue){

cout<<"Exception caught subtraction="<<thrownValue<<endl;

cout<<endl<<endl;

system("pause");

}
Q: Create a program which will through exception by defining multiple catch statements.

#include<iostream>

#include<conio.h>

using namespace std;

void Function(int value){

try{

value;

if (value==0)

throw value;

else if(value>0)throw 'p';

else if(value<0) throw 0.0;

cout<<"*** try block ***\n";

}
catch(char ThrownValue){

cout<<"Caught a positive value \n";

catch(int ThrownValue){

value;

cout<<"Caught a null value\n";

catch(double ThrownValue){

cout<<"Caught a negative value \n";

int main( ){

cout<<"Demo of multiple catches\n"; Function(0);

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>

using namespace std;

void Subtract(int FirstValue,int SecondValue){

cout<< "inside function subtraction \n";

try{

if(FirstValue==0)

throw FirstValue;

else

cout<<"Subtraction ="<<FirstValue-SecondValue;

catch(int){

cout<<"Caught null value \n";throw;

}
cout<<"*** End of Subtract function***\n \n";

int main(){

cout<<"\n inside function main\n";

try{

Subtract(8, 5);

Subtract(0, 8);

catch(int){

cout<<"caught null inside main \n";

cout<<"end of function main\n";

cout<<endl<<endl;

system("pause");

}
Q: Practice the above mentioned examples

#include<iostream>

#include<conio.h>

using namespace std;

int main(){

int ConditionVariable;

cout<<"condition variable ";

cin>>ConditionVariable;

try{

if(ConditionVariable > 0){

cout<< "absolute number ="<<ConditionVariable << "\n";

else{

throw(ConditionVariable);

catch(int thrownValue){

cout<<"Exception caught absolute value =" <<thrownValue<< endl;

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>

using namespace std;

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>

using namespace std;

class queue{

public:

int size;

int front ,rear;

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";

for(int i=front + 1 ;i<=rear;i++)

cout<<a[i]<<" ";

}
int main()

int sz,x,ch;

cout<<"\nEnter queue size:";

cin>>sz;

queue s(sz);

try

do

cout<<"\nEnter choice:\n1 for insert\n2 for remove\n3 for display\n4 to exit\n";

cin>>ch;

switch(ch)

case 1:cout<<"\nEnter a no.:";

cin >>x;

s.insert(x);

break;

case 2:s.remove();

break;

case 3:s.display();

break;

case 4:return 0;

break;

default:

cout<<"Enter a valid choice.";


}

}while(1);

catch(int a)

if(a==s.size-1)

cout<<"\nOverflow\n";

catch(char c)

if(c=='u')

cout<<"\nUnderflow.";

return 0;

You might also like