In this lesson, you will learn,
Anomalies include such as
Exceptions are classified into synchronous and asynchronous exceptions.
The exception occurs during the program execution due to some fault in the input data, which is called a synchronous exception.
Example – Out of range, division by zero, arithmetic exception, overflow, or underflow.
The exceptions caused by events or faults unrelated to the program and beyond the program’s control are called asynchronous exceptions.
Example – Disk failure, hardware malfunctions, or keyboard interrupt.
Exception handling mechanism provides a means to detect and report an exception circumstances. The following are the tasks performed by the mechanism.
The following three important keywords are used to handle exceptions in C++.
The following Figure shows the flow of the exception-handling mechanism.
The general form of the try-catch block is as follows.
try{
Statement 1;
Statement 2;
-----------
Statement N;
if(failure){
throw(Object); //throw an exception
}
}
catch(Object){
Statement 1;
Statement 2;
-----------
Statement N;
}
//Next statements
Note: The catch block that handles the exception must immediately follow the try block.
When the try block throws an exception, the program leaves the control from the try block and enters the catch block to handle the exception. Exceptions are simply objects used to communicate information about the problem.
//ExceptionEx1.cpp
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter any two numbers:n";
cin>>a>>b;
try
{
if (b==0)
throw b;
else
cout<<"Result(a/b)="<<a/b<<endl;
}
catch(int x){
cout<<"2nd operand can't be 0"<<endl;
}
cout<<"End of the program";
}
First Run
Enter any two numbers:
7
3
Result(a/b)=2
End of the program
The output of the first run shows successful execution. If no exception is thrown in the try block, the control is transferred to the first statement after the catch block.
Enter any two numbers:
7
0
2nd operand can't be 0
End of the program
In the second run, the value of the second operand(denominator) is 0. The try block detects an exception and is thrown by the throw keyword using an object b i.e. throw b. Since an object is of int type, the catch statement containing an int type argument i.e. catch(int x) handles the exception and displays the statements.
Note: When the type of object thrown does not match the arg-type in the catch statement. Then the program is aborted and terminated and that exception will not be handled by the catch block.
The following code shows that the program is aborted if the catch statement does not have a matching argument with the type of object thrown.
Change the type of argument in the catch block(int->char) in the above program.
catch(char x){
cout<<"2nd operand can't be 0"<<endl;
}
Enter any two numbers:
7
0
terminate called after throwing an instance of 'int'
Note: When no exception is detected and thrown, the control goes to the first statement immediately after the catch block.
An exception can be thrown from outside the try block as long as it is thrown by a function that is called from within the try block.
return-type function-name(arg list){ //Function with exception
........
throw (object); //throw exception
........
}
........
........
try{
........
//function calling
........
}
catch(arg-type){
//Handle exception
}
.......
//ExceptionEx2.cpp
#include<iostream>
using namespace std;
void divide(int x, int y){
cout<<"Inside function......"<<endl;
if (y!=0){
cout<<"Result(a/b)="<<x/y<<endl;
}
else{
throw y;
}
}
int main(){
int a,b;
cout<<"Enter any two numbers:n";
cin>>a>>b;
try{
cout<<"Inside try block...."<<endl;
divide(a,b);
}
catch(int i){
cout<<"Exception caught: Divide by zero error";
}
return 0;
}
First Run
Enter any two numbers:
11
3
Inside try block....
Inside function......
Result(a/b)=3
Second Run
Enter any two numbers:
5
0
Inside try block....
Inside function......
Exception caught: Divide by zero error
A try block can be localized to a function. When this is the case, each time the function is entered, the exception handling relative to that function is reset.
//ExceptionFuncEx1.cpp
#include <iostream>
using namespace std;
// Localize a try/catch to a function.
void ExCheck(int test){
try{
if(test) throw test;
}
catch(int i) {
cout << "Caught Exception #: " << i << 'n';
}
}
int main(){
cout << "Startn";
ExCheck(1);
ExCheck(2);
ExCheck(0);
ExCheck(3);
cout << "End";
return 0;
}
Start
Caught Exception #: 1
Caught Exception #: 2
Caught Exception #: 3
End
As you can see, three exceptions are thrown. After each exception, the function returns. When the function is called again, the exception handling is reset.
Great !!
amazingg!!
You must be logged in to submit a review.