Thursday, October 14, 2010

Valid use of GOTO?!

I think I may well have come across a valid use of GOTO...
I'm thinking about fault tolerance - perhaps you have a dodgy wireless connection and you want to poll a server once a day by calling a webservice. What happens if that one time a day you go to call the service there are communication problems? The following code adds some fault tolerance for a situation like this.


int limit = 5;
int attempts = 0;

start:
attempts++;

try
{
service.SomeMethod();
}
catch (WebException ex)
{
if (attempts < limit) goto start;
throw;
}