C++ try/catchLast Updated : 14 Jan 2026 In C++ programming, exception handling is performed using try/catch statements. The C++ try block is used to place the code that may occur exception. The catch block is used to handle the exception. SyntaxIt has the following syntax: C++ Simple Try/Catch ExampleLet us take an example to illustrate the try/catch example in C++. ExampleCompile and RunOutput: Exception caught: Division by zero is not allowed!! Explanation In this example, the try block attempts to divide n1 by n2, but because n2 is zero, an exception is thrown with the message "Division by zero is not allowed!". The catch block handles this exception and shows the error message, which prevents the program from crashing. Try BlockIn 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 BlockIn C++, 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.
Handling All Exceptions with catch(...) in C++In C++, 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. SyntaxIt has the following syntax: C++ Example of Handling all Exception using Catch()Let's take a look at a C++ example of handling all exceptions with a catch() function. ExampleCompile and RunOutput: 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. C++ Exception Handling Using Multiple Catch BlocksIn C++, 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. SyntaxIt has the following syntax: C++ Exception Handling Example using Multiple Catch BlockLet's take a look at a C++ example of exception handling using multiple catch blocks. ExampleCompile and RunOutput: 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 FunctionsIn C++, 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. C++ Example of Try/Catch in FunctionsLet us take a C++ example to illustrate the try-catch in function. ExampleCompile and RunOutput: 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 BlocksIn C++, 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. SyntaxIt has the following syntax: C++ Nested try/catch block ExampleLet us take an example to illustrate the Nested try/catch block in C++. ExampleCompile and RunOutput: 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. Next TopicC++ User-Defined Exceptions |
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