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.
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.
It has the following syntax:
In this syntax,
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.
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.
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.
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.
It has the following syntax:
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.
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.
It has the following syntax:
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.
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.
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.
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.
It has the following syntax:
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.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India