C++ Lab Experiment
C++ Lab Experiment
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;
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;
}
return 0;
}
Output :
Thank You