Tricky exception handling
-
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?
-
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?
Why not a simple loop?
bool success;
do
{
try
{
success = true;
// Do your thing
}
catch(OleDbException)
{
success = false;
}
}while(!success);Charlie if(!curlies){ return; }
-
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?
Quimbly wrote: I'm using goto, which is never a good idea. A well placed goto is worth 1000 whiles :p In your case I see nothing wrong. In fact using goto's within try/catch/finally and switches makes alot of sense if well placed :) I for one is not gonna try figure out how to loop thru something complex. Sure you can perhaps 'refactor' the metohd, but that isnt allways the better idea either. If you want some reassurance of got usage, read a bit of Linux kernel code before bed tonite :laugh: top secret
Download xacc-ide 0.0.3 now!
See some screenshots