0% found this document useful (0 votes)
45 views5 pages

C++ Exp 3.1

This document describes two experiments on exception handling in C++. The first experiment involves writing a program to handle a divide by zero exception. It defines variables, performs division in a try block, and catches and displays an error message for a divide by zero exception. The second experiment implements exception handling to test throw restrictions. It defines a handling function with different data types thrown in a try block and caught generically or specifically in catch blocks. Both programs output messages indicating success or the caught exception.

Uploaded by

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

C++ Exp 3.1

This document describes two experiments on exception handling in C++. The first experiment involves writing a program to handle a divide by zero exception. It defines variables, performs division in a try block, and catches and displays an error message for a divide by zero exception. The second experiment implements exception handling to test throw restrictions. It defines a handling function with different data types thrown in a try block and caught generically or specifically in catch blocks. Both programs output messages indicating success or the caught exception.

Uploaded by

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

EXPERIMENT NUMBER : 3.

STUDENT’S NAME : SAKSHI KUMARI


STUDENT’S UID : 21BCS9402
BRANCH : B.E CSE
CLASS & SECTION : 217 – B
SEMESTER : 2
SUBJECT : OBJECT ORIENTED PROGRAMMING USING C++ LAB

AIM: Learning How To Use Exception Handling in C++ Programming .

PREREQUISITES : Basic Knowledge of Exception Handling in C++ Programming .

LIST OF SUB PROGRAMS :

( 1) Write a Program to perform exception handling for Divide by zero Exception

ALGORITHM:

 Step 1: Start the program.


 Step 2: Declare the variables a,b,c.
 Step 3: Read the values a,b,c,.
 Step 4: Inside the try block check the condition. (a. if(a-b!= ...
 Step 5: Catch the exception and display the appropriate message.
 Step 6: Stop the program.

PROGRAM’S CODE :

#include <iostream>
using namespace std;

double division(int a, int b)


{if( b == 0 ) {
throw " Division by zero condition!";
}
return (a/b);
}

int main ()
{
cout<<"\n NAME : SAKSHI KUMARI \n UID : 21BCS9402\n\n";
int x = 50;
}
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB SUBJECT CODE-CSP-152
int y = 0;
double z = 0;

try {
z = division(x, y);
cout<< z <<endl;
}

catch (const char* msg)


{cerr<< msg <<endl;
}
return 0;

}
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB SUBJECT CODE-CSP-152
OUTPUT :

( 2) Write a Program to implement the exception handling with the functionality oftesting
the throw restrictions.

ALGROTHIM :

Step 1: start

Step 2:create a function (named handling)

Step3 :use exception handling (try, throw, catch) concept for finding run time error.
Step 4: in try I take the value of test in int ,float and char
Step5:then throw detect the error and send it to catch
Step6:in catch we caught error by using catch(…) (if data type is not given )
Step 7:stop

PROGRAM’S CODE :

#include <iostream>
#include <conio.h>

using namespace std;

void handle(int test)


{
try {
if (test==0)
{
throw test;
}
if (test==1)
{
throw 'a';
}
if (test==2)
{
throw 123.23;
}
}
catch(int i) {
}
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB SUBJECT CODE-CSP-152
cout<< " Caught " << i << "\n";
}
catch(...) {
cout<< " Caught one!\n";
}

}
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB SUBJECT CODE-CSP-152
int main( ) {
cout<<"\n NAME :SAKSHI KUMARI \n UID : 21BCS9402\n\n";
cout<< " Start\n";
handle(0);
handle(1);
handle(2); cout<<
" End"; return 0;
}

OUTPUT :

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB SUBJECT CODE-CSP-152

You might also like