Mothra Posted November 11, 2004 Posted November 11, 2004 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? Quote Being smarter than you look is always better than looking smarter than you are.
*Experts* Nerseus Posted November 12, 2004 *Experts* Posted November 12, 2004 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 Quote "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* DiverDan Posted November 12, 2004 *Experts* Posted November 12, 2004 You can also recurse the function in the catch section Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
*Experts* Nerseus Posted November 12, 2004 *Experts* Posted November 12, 2004 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 Quote "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* DiverDan Posted November 12, 2004 *Experts* Posted November 12, 2004 Good point Nerseus! Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
John_0025 Posted November 16, 2004 Posted November 16, 2004 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. :) Quote
*Experts* Nerseus Posted November 17, 2004 *Experts* Posted November 17, 2004 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 Quote "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
mskeel Posted November 19, 2004 Posted November 19, 2004 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. Quote
samsmithnz Posted November 19, 2004 Posted November 19, 2004 Yeh I agree, you'll notice then whenever your program throws an exception it 'hangs' for a second before throwing the exception. Quote Thanks Sam http://www.samsmith.co.nz
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.