MODULE 5
Pointers, Arrays and Exception Handling
Classname Pointername;
Eg. Demo *P;
class employee
{
int id;
float salary;
public:
void accept()
{
cout<<”Enter details of employee”;
cin>>id>>salary;
}
void display()
{
cout<<”Employee’s id :”<<id;
cout<<”Employee’s salary :”<<salary;
}
};
Demo O;
Demo *P=&O;
P->accept();
P->display();
int arr[] = { 10, 20, 30, 40 } |
Exception Handling Mechanism
(try, catch and throw)
☞ Exception is handled in C++
● C++ supports exception handling mechanism with the help of keywords : try, throw, catch.
Table: Exception handling blocks
Exception Handling | ||
try | catch | throw |
The statements that may generate exception are written within try block. | This block is exception handler. Exception thrown from try block are handled by catch block. | It is used to throw exception, when exception is detected in try block. |
● The general format of exception handling is as follows :
try
{
//statements that may generate errors
}
catch(data_type arg)
{
// statements to handle exceptions
}
● catch block must immediately follow try block.
● If exception is raised in try block the control automatically goes to catch block.
void main()
{
cout<<”start of program\n”;
try
{
throw 100; // exception is raised
cout<<”Hello world\n”; // this statement will not be executed.
}
catch(int i)
{
cout<<”Exception occurs\n”;
cout<<”Value is”<<i<<endl;
}
}
● Exception is thrown using “throw” keyword. It takes following form :
throw exception;
Example
throw 100;
throw x;
● Throw must be executed from within try block or from any function called from within try block.
try
{
--
throw x; //within try block
--
}catch(int i)
{
--
}
void data()
{
throw x; //called from function within try block
}
:
:
try
{
--
data();
--
}
catch(int i)
{
--
}
⮚ Program
Program to illustrate Exception Handling in C++.
#include<iostream>
using namespace std;
int main()
{
cout<<”start of program\n”;
try // Step 1
{
cout<<”start of try block\n”;
throw 10; //Step 2
cout<<”Hello world\n”;
// this statement will not be executed.
}
catch(int i) //Step 3
{
cout<<”Exception occurs\n”;
cout<<”Value is”<<i<<endl;
}
cout<<”End of program”;
return 0 ;
}
Explanation
● Exception is thrown using “throw 10”. As throw is to be written within try it appears in try block. See Step 1 and 2.
cout<<”start of program\n”;
try
{
cout<<”start of try block\n”;
throw 10;
cout<<”Hello world\n”;
// this statement will not be executed.
}
● As soon as exception is thrown control is transferred to catch block and statements written inside catch are executed. See Step 2 and 3.
catch(int i)
{
cout<<”Exception occurs\n”;
cout<<”Value is”<<i<<endl;
}
● As “throw 10” passes control to catch block; statements after “throw 10” in try block are not executed.
● So the output of above program will be :
Start of program
Start of try block
Exception occurs
Value is 10
End of program
⮚ Program
Program to illustrate multiple catch blocks.
#include<iostream> using namespace std;
void data(int x) { try { if(x) throw x; else throw ‘x’; } catch(int x) { cout<<”Caught integer exception”; } catch(char x) { cout<<”Caught character exception”; } } int main() { data(100); data(0); data(200); return 0 ; } |
//try block
// Step 1
// Step 2
// Step 3 Catch block to handle integer exception
// Step.4 Catch block to handle character exception |
Explanation
● Here try block is designed to throw integer and character exception. See Step1, 2.
try
{
if(x)
throw x;
else
throw ‘x’;
}
● If integer exception is thrown then catch corresponding to integer exception is executed. See Step 3.
catch(int x)
{
cout<<”Caught integer exception”;
}
● If character exception is thrown then catch corresponding to character exception is executed. See Step 4.
catch(char x)
{
cout<<”Caught character exception”;
}
Hence the output of above program will be :
Caught integer exception
Caught character exception
Caught integer exception
● If we throw double exception then above program will not be able to handle as catch block corresponding to double exception is not in a program.
● So either we can design catch block corresponding to double exception or we can design catch block to catch all exceptions instead of certain type.
● catch block to catch all exceptions is written as follows :
catch(..)
{
//statements to handle exceptions
}
● Hence program can be modified as follows :
#include<iostream> using namespace std; void data(double x) { try { if(x) throw x; else throw ‘x’; //Catch block to handle all types of exception } catch(…) { cout<<”Exception caught”; }
int main() { data(100); data(0); data(10.5); return 0 ; } |
//try block
// Step 1
// Step 2
// Step 3 |
Explanation
● “try” block throws exception with the help of “throw” statement. See Step 1, 2.
try
{
if(x)
throw x;
else
throw ‘x’;
}
● This program throws integer, character and double exception see main function. All these exceptions are handled by single block catch(…). See Step 3.
catch(…)
{
cout<<”Exception caught”;
}
● The output of above code will be
Output
Exception caught
Exception caught
Exception caught
● If we use catch(…)with other catch block then it must be written after all catch blocks.
try
{
if(x)
throw x;
else
throw ‘x’;
}
catch(int x)
{
cout<<”Caught integer exception”;
}
catch(char x)
{
cout<<”Caught character exception”;
}
catch(…) // written after all other catch blocks
{
cout<<”Exception caught”;
}
● If catch(…)is written before other catch blocks then other catch blocks will not be executed.
try { if(x) throw x; else throw ‘x’; } catch(…) |
|
{ |
|
cout<<”Exception caught”; | // all exceptions will be handled by this |
} | catch block |
|
|
|
|
|
|
catch(int x) | // these catch blocks will not be executed. |
{ |
|
cout<<”Caught integer exception”; |
|
} |
|
catch(char x) |
|
{ |
|
C out<<”Caught character exception”; |
|
} |
|
⮚ Program
Program in C++ to handle "divide by zero" exception
#include<iostream> using namespace std;
void data(int x, int y);
int main() { int a,b,c; cout<<“Enter values of a and b”; cin>>a>>b; data(a,b); } void data(int x, int y) { try { if(y==0) throw y else c=x/y; cout<<c; } catch(int i) { cout<<“divide by zero exception”; cout<<“Enter number greater than ”<<i; } return 0 ; } |
//try block
//divide x by 0
//Catch block to handle Divide by zero exception
|
● C++ provides standard exceptions defined in <exception>.
● These are as follows
1. std:: exception
It is parent class of all exceptions.
2. std::range_error
It occurs when we try to store a value out of range.
3. std::underflow_error
It occurs when arithmetic underflow occurs.
4. std::overflow_error
It occurs when arithmetic overflow occurs.
5. std::invalid_argument
It occurs due to invalid arguments.
● Exception class is designed specifically to be thrown as an exception.
● We can specify exception class within a class.
class Example
{
int a,b;
public:
class Demo // exception class
{
}
:
:
};
● Here the body of exception class is empty. However we can write statements in it. This class connects a throw statement with catch block. The statement,
throw Demo();
invokes implicit constructor of Demo class and creates an object of it.
throw Demo();
passes control to exception handler catch. It is written as follows :
Exception class
catch(Example::Demo)
{
Exception Handling code
}
● Using this exception class we can return the description of problem encountered in program.
void main()
{
cout<<”start of program\n”;
try
{
throw 100; // exception is raised
cout<<”Hello world\n”; // this statement will not be executed.
}
catch(int i)
{
cout<<”Exception occurs\n”;
cout<<”Value is”<<i<<endl;
}
}
#include<iostream> using namespace std;
void data(int x) { try { if(x) throw x; else throw ‘x’; } catch(int x) { cout<<”Caught integer exception”; } catch(char x) { cout<<”Caught character exception”; } } int main() { data(100); data(0); data(200); return 0 ; } |
//try block
// Step 1
// Step 2
// Step 3 Catch block to handle integer exception
// Step.4 Catch block to handle character exception |