Tricky exception handling

  • Thread starter Thread starter Quimbly
  • Start date Start date
Q

Quimbly

Does anyone know best practices (i.e. a tried and true method) for
this problem? I want to re-attempt an operation which caused on
exception, and do so an arbitrary number of times. For example, I
want to try connecting to a database multiple times before giving
up.

I have a solution (in C#), but I suspect it's not the best way to do
it :

RETRY:
try
{
dbAdapter.Fill(dataSetToFill,
statementCriteria.TableName);
}
catch (System.Data.Odbc.OdbcException dbe)
{
x++;
if (x>max)
{
throw new DMTException("************** ODBC
Error >>>" + x.ToString(), dbe);
}
else
{
goto RETRY;
}
}


This seems to work, but I'm using goto,
which is never a good idea.

Any suggestions?
*---------------------------------*
Posted at: http://www.GroupSrv.com
Check: http://wwww.HotCodecs.com
*---------------------------------*
 
Does anyone know best practices (i.e. a tried and true method) for
this problem? I want to re-attempt an operation which caused on
exception, and do so an arbitrary number of times. For example, I
want to try connecting to a database multiple times before giving
up.

Why not simply use a while loop:

bool done = false;
while (!done)
{
try
{
// Do something
done = true;
}
catch
{
x++;
if (x>max)
{
// Throw your custom exception
}
}
}
 
Back
Top