Basics of exception handling

Share the Article

Exception Handling: When the program encounters an unexpected situation where it do not know how to handle the error, then it throws the control back to a larger concept (this is called throwing an exception). It is then responsibility of larger concept to catch the exception and gracefully handle the situation.

Syntax:

throw 5; //throws an int throw 6.2; //throws a double throw myclass(); //throws an object throw abc(3); //throws an object

Example:

#include <iostream> using namespace std; int main() { try { cout << "Starting try block" << endl; throw 4.3; cout << "Ending try block" << endl; } catch (double d) { cout << "Exception in catch(double) : d = " << d << endl; } return 0; }

The try block is an ordinary scope, preceded by the keyword try. This the place where different function calls and C++ statement are tried. Whenever any exception is thrown with “throw” keyword, the control passes to catch block which lies immediately below the try block.

Each catch block (exception handler) is like a little function that takes a single argument of one particular type

Multiple catch blocks can be used for exception handling, the exception will caught by first catch block whose argument matches the type of exception thrown.

#include<iostream> using namespace std; int main() { try { cout << "Starting try block" << endl; throw 4.2; cout << "Ending try block" << endl; } catch (int i) { cout << "Exception caught in first catch(int) : i = " << i << endl; } catch (double d) { cout << "Exception caught in second catch(double) : d = " << d << endl; } return 0; }

The output is:

Starting try block Exception caught in second catch(double) : d = 4.2

Here, the first catch block is accepting exception of type int, so that block was bypassed, the thrown exception type double matched the second block.

No compatible catch : However, if no catch block compatible with type of exception thrown is found, then program will terminate.

#include <iostream> using namespace std; int main() { try { cout << "Starting try block" << endl; throw 4; cout << "Ending try block" << endl; } catch (double d) { cout << "Exception caught in catch(double) : d = " << d << endl; } return 0; }

The output is:

Starting try block terminate called after throwing an instance of 'int' Aborted (core dumped)

Catching exception of any type

The compiler calls catch block with ellipses, when it cannot find any properly matching catch among the existing catch. The catch block with ellipses is the default handler which catches any kind of exception. This block has ellipses in the argument list. The programmer must place this handler in the last position compared to other catch blocks.

#include<iostream> using namespace std; int main() { try { cout << "Starting try block" << endl; throw 'E'; cout << "Ending try block" << endl; } catch (int i) { cout << "Exception caught in first catch(int) : i = " << i << endl; } catch(...) { cout << "Exception caught in default catch(...)" << endl; } return 0; }

The output is:

Starting try block Exception caught in default catch(...)

Default catch position: The compiler shall throw an error the program do not places the ellipses catch handler in last position. See following error.

#include<iostream> //main header using namespace std; //for namespace int main() { try { cout << "Starting try block" << endl; throw 'E'; cout << "Ending try block" << endl; } catch(...) //NOT AT LAST POSITION { cout << "Exception caught in default catch(...)" << endl; } catch (int i) { cout << "Exception caught in first catch(int) : i = " << i << endl; } return 0; }

Output Warning:

compiler error when catch block with  ... is not in last position

Main Funda: First compatible catch-handler block will catch the exception

Related Topics:

Why a destructor should never throw exception?
What is the problem with setjmp( ) & longjmp( ) ?
Why should we catch an exception object, using reference-parameter?
What happens when exception thrown from a constructor?
Re-throwing an exception
What are function level try-catch blocks
Understanding exception specification for functions
Explaining C++ casts
How pointer to class members are different ?
What is reference collapsing?
How to make a class object un-copyable?

Share the Article

Leave a Reply

Your email address will not be published. Required fields are marked *