0% found this document useful (0 votes)
9 views8 pages

C++ Lab Experiment

The document discusses exception handling in C++ programming, explaining the concepts of try, catch, and throw keywords. It provides examples of code that demonstrate how to handle division by zero and multiple exceptions. The document concludes with a demonstration of a multi-catch program that handles different types of exceptions.

Uploaded by

riksohom3
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)
9 views8 pages

C++ Lab Experiment

The document discusses exception handling in C++ programming, explaining the concepts of try, catch, and throw keywords. It provides examples of code that demonstrate how to handle division by zero and multiple exceptions. The document concludes with a demonstration of a multi-catch program that handles different types of exceptions.

Uploaded by

riksohom3
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/ 8

C++ LAB

EXPERIMENT

Topic : Exception
Handling

Submitted By :
Sohom Ghorai
190301120001
B.Tech CSE , 3rd Sem
CUTM,BBSR
Exception:
An exception is a unexpected problem or error that
arises during the execution of a program .
So during compile time there is no error but at the
runtime it shows error.
Handling :
There are three keywords related to exception hadling.
Try,Catch,Throw
 Try :
A block of code which may cause exception is
placed inside try block.It’s followed by one or
more catch blocks.If an exception occurs , it’s
thrown from the try block.
 Catch :
This block catches the exception thrown from the
try block . Code to handle the exception is written
inside it .
 Throw :
A program throws an exception when a problem
shows up.This is done using throw keyword.
Program and explanation :
Code :
#include<iostream>
using namespace std;

int main()
{
int num,den,result;
cout<<"Enter numerator and denominator : ";
cin>>num>>den;
result = num/den;
cout<<"Division Result is "<<result;
return 0;

This is an simple code which will divide two numbers


and shows the result so lets execute it.

So its working fine . But what if we take the


denominator value 0 .
So in this case the program crashes and it showing any
output . So it’s an exception case and we have to handle
it .

So after few modification the code is done .


Code:
#include<iostream>
using namespace std;

int main()
{
int num,den,result;
cout<<"Enter numerator and denominator : ";
cin>>num>>den;
try{
if(den==0)
{
throw den;
}
result = num/den;
}
catch(int ex)
{
cout << "Exception divided by Zero is not allowed . "<<ex;
}

cout<<"Division Result is "<<result;


return 0;

Now we are able to handle this exception and show a


proper message to user about what to do .
Multi-Catch Program
Code :
#include<iostream>
using namespace std;
void test(int x)
{
try
{
if(x==1)throw x;
else if(x==0)throw 'x';
else if(x==-1)throw 1.0;
cout<<"End of try-block \n";
}
catch(char c)
{
cout<<"Caught a character \n";
}
catch(int m)
{
cout<<"Caught an integer \n";
}
catch(double d)
{
cout<<"Caught a double \n";
}
cout<<"End of try-catch system \n\n";
}
int main()
{
cout<<"testing Multiple Catches \n";
cout<<"x ==1 \n";
test(1);
cout<<"x ==0 \n";
test(0);
cout<<"x ==-1 \n";
test(-1);
cout<<"x ==2 \n";
test(2);

return 0;
}

Output :
Thank You

You might also like