Inside a for each loop, I have a try catch activity. I also have a bool isError=false before moving forward to the “try”. In the “catch”, I have a log message and bool isError=true.
My question is, if I catch an exception and isError=true, what do I place in the “finally” so that the loop continues even if an exception is caught?
As you are saying that you are using try catch within a for each scope and you wants to continue your loop even any exception will occur.
So if you are still using the try catch within the loop scope it will always run that even exception will occur. it is upto you how you deal with exception in your way. So in your case you are also handling exception by using flags.
For example purpose:
for (int i = 0; i < 10; i++)
{
try
{
if (i == 2 || i == 4)
{
throw new Exception("Test " + i);
}
}
catch (Exception ex)
{
// generate a log and flag set.
}
}
here I am attaching a demo sample artifact for your understanding. just see the logic.
**Note: It is not mandatory to add code in finally block. you should add only code within this scope when you wants to execute those instructions anyhow if exception will occure or not".
For try-catch understanding you can visit this link as well