C++ Exceptions Handling
An exception is a problem that occurs during the execution of a program or a program encounters run-time anomalies during its execution known as an exception. Exception handling is a process through which runtime anomalies are handled, such as an attempt to divide by zero.C++ provides following specialized three keywords :
try: Whenever a problem occurs, a block of code throws an exception.
catch:A block of code that handle exceptions.
throw:Used to throw run-time anomalies.
Syntax :
If there is an exception encounter in an try block then catch block is used to handle those exceptions. There can be multiple catch blocks to catch different types of exceptions.
try { // protected code } catch( Exception a1 ) { // catch block } catch( Exception a2 ) { // catch block } catch( Exception aN ) { // catch block }
C++ Standard Exceptions
There is a a list of standard exceptions in C++ which we can use in our programs.
Exception | Description |
---|---|
std::bad_alloc | The exception can be thrown by new. |
std::exception | A parent class of all the standard C++ exceptions. |
std::bad_cast | The exception can be thrown by dynamic_cast. |
std::logic_error | This type of exception can be theoretically detected by reading the code. |
std::bad_exception | It handle unexpected exceptions in a C++ program. |
std::bad_typeid | The exception can be thrown by typeid. |
std::out_of_range | This type of exception thrown by the 'at' method. |
std::length_error | This type of exception thrown when a too big std::string is created. |
std::domain_error | This type of exception thrown when a mathematically invalid domain is used. |
std::invalid_argument | This type of exception thrown due to invalid arguments. |
std::underflow_error | This type of exception thrown if a mathematical underflow occurs. |
std::overflow_error | The type of exception thrown if a mathematical overflow occurs.. |
std::runtime_error | This type of exception cannot be theoretically detected by reading the code |
std::underflow_error | This type of exception thrown if a mathematical underflow occurs. |
Throwing Exceptions
An example of throwing an exception when dividing by zero condition occurs
Example :
double division(int a, int b) { if( b == 0 ) { throw "Division by zero condition!"; } return (a/b); }
In the above example if a = 0 then the code will be compiled successfully but when b = 0 then the "if" condition is correctly evaluated through which an exception occur "Division by zero condition", and that exception will be thrown and will be handled by Catch block.
Catching Exceptions
The catch block is used to handle the exception thrown by the try block, the different exception will have different catch block, So what type of exception you want to catch and this is determined by the exception declaration that appears in parentheses, so we have to specify that the catch block should handle any type of exception that is thrown by a try block.
Example :
try { // protected code } catch( ExceptionName e ) { // code to handle ExceptionName exception }
Exercise:-
1. Which is used to handle the exceptions in c++?
View Answer
2. Which type of program is recommended to include in try block?
View Answer
Program :-
A program on a division by zero exception
#include <iostream> using namespace std; double division(int x, int y) { if( y == 0 ) { throw "Caught a divide by zero condition in the program!"; } return (x/y); } int main () { int a = 50; int b = 0; double c = 0; try { c = division(a, b); cout << c << endl; } catch (const char* txt) { cerr << txt << endl; } return 0; }
Output :
Caught a divide by zero condition in the program!
Visit :
Discussion