Catch and restore the state on each iteration, even though catching outside will work the same, here you are still in the loop and you can decide whether you want to continue or break the loop.
ALSO: Catching an Exception
is wrong from the beginning (what are you going to do if you catch StackOverflowException
or MemoryLeachException
- EDIT: this is just for example, check documentation to know what you can catch in reality). Catch concrete type of exception which you expect to be thrown.
while(true)
{
try
{
// Do some work repeatedly...
}
catch(FileNotFoundException) //or anything else which you REALLY expect.
{
// I have to reset the status before to continue to do my work
ResetStatus();
//decide here if this is till OK to continue loop. Otherwise break.
}
}
for those very smart in the comments: Why don't catch general exception