How Do I Continue to the Next Iteration of a for Loop After an Exception is Thrown in C++? Last Updated : 22 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, when an exception is thrown, the normal flow of the program is disrupted and we need to take care of it. In this article, we will learn how can we continue to the next iteration of a for loop after an exception is thrown in C++. Continue Loop After an Exception is Thrown in C++To continue to the next iteration of a for loop after an exception is thrown, we can enclose the code that might throw an exception within a try block and handle the exception in a catch block inside the loop itself. Then proceed to the next iteration using the continue statement after handling the exception. C++ Program to Continue to the Next Iteration of a Loop After Throwing an ExceptionThe below example demonstrates how we can continue to the next iteration of a loop after an exception is thrown in C++. C++ // C++ program to continue to the next iteration of a for // loop after an exception is thrown in C++ #include <iostream> #include <stdexcept> using namespace std; int main() { // for loop for (int i = 1; i <= 5; ++i) { try { // Your code that might throw an exception if (i == 3) { // Throw a runtime_error exception throw runtime_error("Exception occurred!"); } // Continue with the rest of the loop body if no // exception is thrown cout << "Iteration " << i << endl; // Print the iteration number } catch (const runtime_error& e) { // Handle the exception cerr << "Caught exception: " << e.what() << endl; // Continue to the next iteration of the loop continue; } } return 0; } Output Iteration 1Iteration 2Caught exception: Exception occurred!Iteration 4Iteration 5Explanation: In the above example the code that might raise an exception is contained in the try block and the catch block handles exceptions and to continue to the next iteration continue statement is used. Comment More infoAdvertise with us Next Article How to Throw a Custom Exception in C++? 2111cs0yf7g Follow Improve Article Tags : C++ Programs C++ cpp-exception C++-Exception Handling CPP Examples +1 More Practice Tags : CPP Similar Reads How to Throw a Custom Exception in C++? In C++, exception handling is done by throwing an exception in a try block and catching it in the catch block. We generally throw the built-in exceptions provided in the <exception> header but we can also create our own custom exceptions.In this article, we will discuss how to throw a custom e 2 min read How to Throw a Custom Exception in C++? In C++, exception handling is done by throwing an exception in a try block and catching it in the catch block. We generally throw the built-in exceptions provided in the <exception> header but we can also create our own custom exceptions.In this article, we will discuss how to throw a custom e 2 min read How to Throw a Custom Exception in C++? In C++, exception handling is done by throwing an exception in a try block and catching it in the catch block. We generally throw the built-in exceptions provided in the <exception> header but we can also create our own custom exceptions.In this article, we will discuss how to throw a custom e 2 min read How to Throw and Catch Exceptions in C++? In C++, exception handling is a mechanism that allows us to handle runtime errors and exceptions are unusual conditions that occur at runtime. In this article, we will learn how to throw and catch exceptions in C++. Throw and Catch Exceptions in C++In C++ exceptions can be "thrown" when an error occ 2 min read How to Find Last Occurrence of an Element in a List in C++? In C++, std::list represents a doubly linked list and the last occurrence of an element in a list refers to the last time that element appears in the list when traversed from the beginning. In this article, we will learn how to find the last occurrence of a specific element in a list in C++. Example 3 min read How to Throw an Exception in C++? In C++, exception handling is a mechanism that allows us to handle runtime errors and exceptions are objects that represent an error that occurs during the execution of a program. In this article, we will learn how to throw an exception in C++. Throw a C++ ExceptionThrowing an exception means sendin 2 min read Like