Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Tricky exception handling

Tricky exception handling

Scheduled Pinned Locked Moved C#
helpcsharpdatabasetutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Quimbly
    wrote on last edited by
    #1

    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?

    C L 2 Replies Last reply
    0
    • 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?

      C Offline
      C Offline
      Charlie Williams
      wrote on last edited by
      #2

      Why not a simple loop?

      bool success;
      do
      {
      try
      {
      success = true;
      // Do your thing
      }
      catch(OleDbException)
      {
      success = false;
      }
      }while(!success);

      Charlie if(!curlies){ return; }

      1 Reply Last reply
      0
      • 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?

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups