VB.net Try Catch Finally

MTSkull

Centurion
Joined
Mar 25, 2003
Messages
151
Location
Boulder, Colorado
How do I resume Execution after I fix the error?

The failure I want to fix occures at gdsData.Tables("MyTable").NewRow(). The path the user took to get here resulted in no data loaded into the Dataset, so I want to load the dummy record into the dataset to allow the add to continue.
Visual Basic:
Try

  gdsData.Tables("MyTable").NewRow()
  gdsData.BeginEdit()
  ....

Catch oException As Exception

  If oException.Message = "Object reference not set to an instance of an object." then

    giCurrentDocID = 1
    funLoadDocData()
    Resume?

  Else

  'something else happened so bail out

  End if

End Try

After I load the Doc Data I want to resume where I left off in this instance.
Also is this the best way to handle the error or could there be a cleaner way to do this?

Thanks
MTS
 
Firstly you would be much better catching the specific exception rather than relying on the error message to identify the problem. Also it would be better if you only put the line that causes the exception in the try block.
. i.e.
Visual Basic:
Try

  gdsData.Tables("MyTable").NewRow()
Catch oException As NullReferenceException
    giCurrentDocID = 1
    funLoadDocData()
    Resume?
catch ex as Exception
    'Something else happened so deal with it here
End Try

gdsData.BeginEdit()
....
 
To prevent this exception from being thrown in the first place,
check to see if the object is equal to a null reference (Nothing)
before using it.

Visual Basic:
If MyObject Is Nothing Then
  ' The object wasn't initialized
Else
  ' The object can be used
End If
 
Thanks,

I tried both methods Just for fun and they worked like a charm. I was looking at some old code and I had used the if odject is nothing then to fix it before so that is what I stuck with.

Thanks From a very stressed out under an impossible deadline programmer!
MTS
 
Back
Top