Language Elements

Object Oriented Concepts

Data Types

Key Words

Arrays

Pointers

Data Structures

Multithreading

Errors and Exception Handling

Interview Questions



Contact



Exceptions and Error Handling in C++


An exception is a situation in which a program encounters unexpected circumstance due to some problem in part of the code, which it is not explicitly designed to handle. In C++ exceptions can be handeled by transfering control to handlers (as the exception occurs). Exception handling separates the part with unexpected problem from rest of the code, making reading and writing of the code easier.

Exception Handling

Exceptions can be handeled by using a try block. Any unexpected errors can be thrown within this block. Immediately after the try block is closed, the catch block starts, with exception handling code. It can be illustrated as shown in the pseudo-code below:

try {
...
...
throw Exception()
...
...
} catch( Exception e)
{
...
...
}

The lowercase character e is reference to the thrown (and caught) Exception object.





Copyright © by Zafar Yasin. All rights reserved.