18.3 EXCEPTION HANDLING BUILDING BLOCKS

Exceptions are of two types: synchronous and non-synchronous. Events like power failure are considered non-synchronous. As a programmer, we have no control over them. Other exceptions are synchronous. They get generated while running our program. They are generated because of our program. A typical example is dividing by zero. In this section, we are basically discussing synchronous exceptions.

Now C++ offers an elegant way to handle these exceptions. It makes our program more robust. It is now possible that programs do not fail on trivial errors. In other words, they become fault tolerant to some extent.

The exception handling revolves around three statements, try, catch and throw.

18.3.1 throw statement ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.