I have a piece of code like the following:
try
{
Work:
while(true)
{
// Do some work repeatedly...
}
}
catch(Exception)
{
// Exception caught and now I can not continue
// to do my work properly
// I have to reset the status before to continue to do my work
ResetStatus();
// Now I can return to do my work
goto Work;
}
Are there better alternatives compared to using goto? Or is this a good solution?
It sounds like you really want a loop. I’d write it as:
You might want to use a
do/whileloop instead; I tend to prefer straightwhileloops, but it’s a personal preference and I can see how it might be more appropriate here.I wouldn’t use
gotothough. It tends to make the code harder to follow.Of course if you really want an infinite loop, just put the
try/catchinside the loop: