I meant resume execution
back to the line where the error occurred, or to a line of your choice. This is very, very handy, yet is missing from C# exception handling.
Consider, for example, the following scenario: I have a VB6 database application (using Jet 4.0). Different users can add new records to a table. The primary key for each new record is computed from within my code so I have something that looks like this:
Code:
[COLOR=blue]Public Sub[/color] AddRecord()
[COLOR=green] ' Get primary key value[/color]
lPrimaryKey = GetNextKey()
[COLOR=green] ' Save the record using a sql statement[/color]
sSQL = "INSERT INTO MyTable ..."
[COLOR=green] ' Execute the changes with connection[/color]
cn.Execute(sSQL)
[COLOR=blue]End Sub[/color]
If two users (in two different computers) attempt to save a new record at, more or less, the same time, the function GetNextKey() may return the same key for both users (I have a series of rules for creating this key but this is not the issue). When this happens, one of the users won't be able to save his record because, obviously, the table does not allow duplicate values for the primary key.
This, in fact happens quite a few times during this data entry process, as records are constantly being created by different users. When this happens, I don't want to tell the user
"Ooops!, your record could not be saved. Please try again later."
You see, what I do is, I inspect the error and if I think it was caused by two users trying to save at the same time,
I resume execution to where the function GetNextKey() is. When GetNextKey() runs again, it will return the correct key and the user will be able to save his record. If in the mean time, another user is saving and the key is the same again, I just resume execution to GetNextKey() again. Now, the user is completely unaware of this. To him, he just saved a new record. Oh, and by the way, I also keep a count of how many times I've resumed for a record, so that I don't resume for ever. After a certain number of times, the user gets the message "An error occured and your record could not be saved. Please try again later."
So, you see, In Visual Basic 6, I could say
Resume Next, Resume, Resume [to label]. These statements, when used wisely, proved to be very useful. With them, you don't have to know
WHERE in code an error occured. By contrast, in C#, in order to resume execution to a place of your choice, you have to first know the block of code where the error might occur before hand and write loops or other form of code that will take care of the resuming business. Knowing which piece of code will throw an exception is not always so obvious as in the example I gave above.
So what do you say?
JC
Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...