Programming Code Examples

How to ReThrow an Exception in C++ Programming2 min read

In C++ if a functions or a nested try-blocks does not want to handle an exceptions, it can rethrow that exceptions to the functions or the outer try-block to handle that exceptions. Syntax for rethrowing and exceptions is as follow:


Following program demonstrate rethrowing and exceptions to outer try-catch block:

Input and output for the above programsas follows:

In the above program, we can see that the exception is raised in the inner try block. The catch-all block catches the exception and is rethrowing it to the outer try-catch blocks where it got handled.

Throwing Exceptions in Function Definition

A function can declare what type of exception it might throw. Syntax for declaring the exceptions that a function throw is as follows:

Following program demonstrate throwing exceptions in a function definition:

Input and output for the above programs are as follows:

In the above programs, the sum() function can throw an exceptions of type int. So, the calling function must provides a catch block for the exception of type int.

Take your time to comment on this article.

Add comment