Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
In a Try-Catch block, is there a way to "ReTry" the procedure if there is an error? For example, say you have a procedure in a Try-Catch that is populating a combobox from a database and something happens that returns no rows of data (something like heavy database server traffic, etc...) but, no error is thrown. Is there a way to attempt the data retrieval from insinde the Try block?
Being smarter than you look is always better than looking smarter than you are.
  • *Experts*
Posted

There's no keyword for what you want, but you can do it programmatically fairly simply. For DB calls and webservice calls, you can usually wrap the call in a function that attempts, say, 3 retries. Here's some pseudo-code:

Function CallDB(string SQL)
   int i = 0;
   do
       try
           Make DB Call
           break; // exits do/while
       catch
           i++
   while(i < 3)
   if i == 3
       // Error - never got through
   end if
End Function

 

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted

You can definitely recurse, but you'll need some kind of limit so you don't retry indefinitely (a bad idea if the SQL is failing because of bad SQL syntax, which would never work). You can enhance the method to take a "count" parameter which is increased on every recursive call or have a static variable, but it's about the same as using a loop.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

I have a similar problem. Records in my database must have unique names. So I catch the exception. Allow the user to rename the record and then I wanted to 'retry'. I was using the goto and label stuff in VB. Not sure if this is now frowned upon in VB.net ;)

 

       Public Sub CallDB()
tryAgain:
           Try
               'Make db call
           Catch ex As uniqueNameException
               Dim errorHandler As New ErrorBox(ex, ErrorBox.buttonTypes.OkCancelBox, "Please enter a new record name.", True)
               If errorHandler.response = ErrorBox.Selected.OkButton Then
                   Me.recordname = errorHandler.InputText
                   GoTo tryAgain
               End If
           End Try
       End Sub

 

Nerseus you seem to have morphed VB and C# together. :)

  • *Experts*
Posted

I was just using pseudo-code, to show the general feel for a "3 try, retry" in a function. But maybe is *should* make my own language!

 

John_0025, for what you want I would suggest a little refactoring to make your code more readable. In particular, the use of goto is generally frowned on, mostly because it generally makes code less readable. Suppose you had two functions instead of one: InsertWithUniqueName and CallDB. The call to InsertWithUniqueName could call CallDB and, if it violated the unique constraint, popup the message and call CallDB again. That way you get some reuse out of the "CallDB" function and you keep the retry code in a different function.

 

For very small functions, you could argue that a label and goto do not make the code less readable, but there's almost always a *more* readable way to write the code that would not use the label and goto. Anything that's more readable is definitely going to be better in the end, especially when you have to go back and touch that code in 6 months.

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Another refactoring point.

 

It also sounds like you are trying to use exception handling to drive the logic of your program. Exceptions should only be used to handle exceptional things. This case sounds like something that is going to happen a lot and you expect to happen.

 

It is generally better practice to check to see if the name is unique first before adding it. The reasons for this are both maintainability and performance. What if the library you are using changes and no longer requries that the name be unique but it is required for your program? Also, Exeption handling is very expensive. If this is somethign you expect to happen often it is faster to check with an if than to catch an exception. And if you are looping (as it looks like you will be) you will see a noticable difference.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...