Tricky exception handling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Quimbly

    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,
    statementCriter ia.TableName);
    }
    catch (System.Data.Od bc.OdbcExceptio n 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
    *---------------------------------*

    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    Best Usenet Service Providers 2025 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.

  • Elp

    #2
    Re: Tricky exception handling

    On 27 Jan 2005 10:53:06 -0600, Quimbly wrote:
    [color=blue]
    > 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.[/color]

    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
    }
    }
    }

    Comment

    Working...