VOOZH about

URL: https://www.geeksforgeeks.org/cpp/continue-to-next-iteration-of-for-loop-after-exception-is-thrown-in-cpp/

⇱ How Do I Continue to the Next Iteration of a for Loop After an Exception is Thrown in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How Do I Continue to the Next Iteration of a for Loop After an Exception is Thrown in C++?

Last Updated : 23 Jul, 2025

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 Exception

The below example demonstrates how we can continue to the next iteration of a loop after an exception is thrown in C++.


Output

Iteration 1
Iteration 2
Caught exception: Exception occurred!
Iteration 4
Iteration 5

Explanation: 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