C++ try and catch Statements (With Examples)

Last Updated : 9 May 2026

The try and catch statements in C++ are used for exception handling. They help handle runtime errors and prevent the program from terminating unexpectedly.

In this chapter, you will learn about try and catch statements in C++, their syntax, working process, and examples of exception handling.

What are try and catch Statements in C++?

In C++, try and catch statements are used for exception handling. They help detect and handle runtime errors so that the program can continue running safely instead of terminating unexpectedly.

The try block contains the code that may generate an exception. If an error occurs inside the try block, the throw statement is used to generate an exception.

The catch block receives and handles the exception generated by the throw statement. It helps display an error message and prevents abnormal program termination.

Syntax

It has the following syntax:

In this syntax,

  • try contains the code that may produce an exception.
  • throw is used to generate an exception.
  • catch is used to handle the exception.

Example of try and catch Statements

In the following example, an exception is generated when division by zero is attempted.

Output:

Exception caught: Division by zero is not allowed!! 

Explanation

In this example, the try block checks whether the divisor is zero. Since n2 is zero, an exception is generated using the throw statement. The catch block handles the exception and displays the error message instead of terminating the program.

Try Block

In C++, the try block is used to wrap the code that can cause an error during runtime. In other words, the try block contains code that could result in runtime errors, such as division by zero, improper memory access, or file handling failures. It enables the software to do potentially risky activities while assuring that errors are graciously handled.

Catch Block

The catch block comes after the try block and is used to handle exceptions that are thrown in the try block. It specifies how certain error kinds should be handled, which eliminates unexpected program termination and allows different forms of exceptions to be handled effectively.

  • In order to handle exceptions, the try/catch keywords are usually used in pairs. The try block contains code that may produce an error, and the catch block specifies how to handle that error if it occurs.
  • Every try block in C++ requires at least one catch block. A try block is required before the catch block. It ensures that exceptions are properly handled and do not cause unexpected program termination.

Handling All Exceptions with catch(...)

If we don't know what kind of exception will be raised, we can use catch(...) to handle all exceptions. It ensures that all exceptions, regardless of type, are properly discovered and handled.

Syntax

It has the following syntax:

Example

Let's take a look at a C++ example of handling all exceptions with a catch() function.

Output:

ERROR!
Error: Division cannot be performed due to a wrong divisor.

Explanation

In this example, we attempt to divide the dividend by divisor while handling potential exceptions. Inside the try block, it checks to see if the divisor is zero and throws an exception if it is; otherwise, the division is performed. The catch() block, which serves as a catch-all handler, intercepts any exception and displays an error message if the division is wrong.

Exception Handling Using Multiple Catch Blocks

We can use many catch blocks after a single try block to handle different types of exceptions. If an unknown exception occurs, the catch(...) block will handle it.

Syntax

It has the following syntax:

Example

Let's take a look at a C++ example of exception handling using multiple catch blocks.

Output:

Enter the index: 3
Enter the numerator: 6
Enter the denominator: 0
ERROR!
Error: Cannot divide by zero!

Explanation

This C++ program divides and stores the results in an array while managing exceptions using numerous catch blocks. It first determines whether the user-provided index is within bounds; if not, it throws an exception. If the denominator is zero, an additional exception is issued, and the final catch catches any unanticipated errors (...) block, which ensures smooth error handling.

Try/Catch in Functions

Exceptions can be thrown inside functions and caught in main() or other calling functions. We can use try-catch blocks inside functions to handle exceptions. When a function throws an exception, the program looks for the nearest catch statement to handle it.

Example

Let us take a C++ example to illustrate the try-catch in function.

Output:

Caught exception: Division by zero Exception!

Explanation

In this example, the function divide(int x, int y) performs division. In the main() function, the divide function is invoked within a try block. As the division by zero is attempted (divide(15, 0)), the function throws an exception, which is caught in the catch statement that handles const char* exceptions.

Nested Try/Catch Blocks

The nested try-catch statements may be defined inside another try block or catch block. It allows us to handle exceptions in those situations where different exception happens in the different code sections.

Syntax

It has the following syntax:

Example

Let us take an example to illustrate the Nested try/catch block in C++.

Output:

Outer try block started
Inner try block started
Caught in inner catch: Division by zero in inner block!
Caught in outer catch: Division by zero in inner block!
It continues after nested try-catch...

Explanation

In this example, we demonstrate nested try-catch blocks where an exception for division by zero is first caught in the inner catch block and then rethrown to be handled by the outer catch. It shows how control flows when exceptions are passed between nested blocks.