C++ IOE Note By Anup sir(IOE Pulchowk blog)
C++ note |
Click Me |
Exception Handling:
It is required to develop a fault tolerance software system. If a software program is provided with exceptional handling mechanism then the software will smoothly run in spite of presence of fault.
Exception handling mechanism generally uses three keywords they are:
try , catch and throw statement.
è The part of code that generate the exception should be placed in try block.
è if any exception occurs then it is thrown using throw keyword
è the exception handling code or mechanism is placed in catch block.
- Example 1 of exception handling in c++ :
- Example of exception handling in oop :
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b,x;
cout<<"Work: a/(a-b)\nEnter the value of a and b"<<endl;
cin>>a>>b;
x=a-b;
try
{
if (x!=0)
{
cout<<"\nThe division is "<<a/x<<endl;
}
else
{
throw(x);
}
}
catch(int y)
{
cout<<"\nException is found"<<"Division can not occur by zero difference"<<y<<""<<y;
}
return 0;
}
exception handling in c++ : |
- Example 2 of exception handling in c++
- Example 2 of exception handling in oop
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float x;
int a;
cout<<"\nEnter number"<<endl;
cin>>a;
try
{
if( a>0)
{
x=sqrt(a);
cout<<"\nSquare root of "<<a<<"is "<<x<<endl;
}
else
{
throw(a);
}
}
catch(int x)
{
cout<<"Square root of negative number "<<a<<" is not possible"<<endl;
}
return 0;
}
Re throwing exception:
if we can not handle the exception completely i.e. caught by catch block then we rethrow that exception by using throw keyword only without using any argument. This is called re throwing exception.
this cause the current expression to be pass into the out of try catch block;
- Example of re throwing exception.in c++:
#include<iostream>
using namespace std;
int main()
{
int a,b,x,c;
cout<<"Enter the value of a and b"<<endl;
cin>>a>>b;
x=a-b;
try{
if(x!=0)
{
cout<<"The resul is "<<(a/x)<<endl;
}
else
{
throw(x);
}
}
catch(int x)
{
cout<<"\nException Found\n";
throw;
}
return 0;
}
Catching all exception:
in c++ there is provision of catching all exception raised in try block of code without considering their data type. This provision is known as catching all exception.
in other word catching all exception will catch all the exception of any data type raised from try block.
- syntax for catching all exception:
catch(…)
{
//handling all type of exception
}
- Example of catching all exception in c++:
#include<iostream>
using namespace std;
void function(int test)
{
try
{
if (test==0)
{
throw(test);
}
else if (test==1)
{
throw(1);
}
else if (test==2)
{
throw(2);
}
else
{
cout<<"\nCalled function other than 0, 1,2";
}
}
catch(...)
{
cout<<"All Exception Caught";
}
}
int main()
{
cout<<"\nCalling function with 0";
function(0);
cout<<"\nCalling function with 1";
function(1);
cout<<"\nCalling function with 2";
function(2);
cout<<"\nCalling function with 3";
function(3);
return 0;
}
Multiple handling
In C++ there is provison of handling multiple exception that is generated from try block
but in the case of multiple handler we use separate catch block to handle exception of each data type.
- Example of multiple handling in c++
catch (int a)
{
cout<<"integer found"<<endl;
}
catch (char ch)
{
cout<<"character found"<<endl;
}
catch (float c)
{
cout<<"float found"<<endl;
}
Ambuiguity:
ReplyDeletewhen two or more base class have same function name and object of publicly derived class calls the function by its name, confusion occurs which is known as ambuigity.
We will include your suggestion very soon
ReplyDeleteSir you need to make some correction in ambiguity inheritance
ReplyDeleteThank for your valuable suggestion we will soon make corrections @metallic heart
Delete