Kerry 发表于 2005-8-26 11:22:43

Actually while it is a common method in C++ it is also available in VB ..

Keith™ 发表于 2005-8-26 11:35:52

I'm not a VB guru, anyway thanks for the hint.

Kerry 发表于 2005-8-26 11:45:54

Here is an example of a useful on error resume next:

Private Function HasElements(ArrayIn As Variant) As Boolean
'this routine will test to see if a 1d array is initialized
   On Error Resume Next
   HasElements = (UBound(ArrayIn) >= LBound(ArrayIn))
End Function

Typically, this is what I do:

Private Sub foo()
On Error GoTo foo_Error
'work goes here
ExitHere:
    'clean up code goes here
    On Error GoTo 0
    Exit Sub
foo_Error:
   'log the error here by inspecting the Err object
    Resume ExitHere
End Sub

Say you where in a loop and you wanted to catch an error in the loop, you could use the above construct with the resume next command, that would place you on the next line following the error.
Here is an example:

'This routine takes a csv string if line handles and returns a collection
'of acadLine's
Private Function stringToHoles(sCol As String, sIdentifier As String) As Collection
Dim lineObj As AcadLine
Dim sHandles() As String
Dim i As Long, upper As Long
Dim colLines As New Collection
On Error GoTo ErrorHandler
If Len(sCol) = 0 Then GoTo ExitHere
sHandles= Split(sCol, ",")
upper = UBound(sTemp)
For i = 0 To upper
    Set lineObj = ThisDrawing.HandleToObject(sHandles(i)) 'this will cause an error if the handle doesn't exist
    If Not lineObj Is Nothing Then
      lineobj.Highlight True
      colLines.Add lineobj
    End If
    Set lineObj = Nothing
Next i
Set stringToHoleCollections = colLines
ExitHere:
    Set colTemp = Nothing
    Erase sTemp
    On Error GoTo 0
    Exit Function
ErrorHandler:
    Select Case Err.Number
      Case -2147467259 'Automation error Unspecified error - Means the object doesn't exist
            Set lineObj = Nothing
            Resume Next
      Case -2145386484 'unknown handle - means the handle doesn't exist in the drawing
            Set lineObj = Nothing
            Resume Next
      Case -2145386420 'object erased continue on
            Set lineObj = Nothing
            Resume Next
      Case Else
    End Select
    Resume ExitHere
End Function

Kerry 发表于 2005-8-26 11:59:17

I'm not a VB guru, anyway thanks for the hint.
resistance is futile, you will be assimilated...

Kerry 发表于 2005-8-26 12:53:48

> In Visual Basic, what does the line "On Error
> Resume Next" instruct the program to do?
My guess:
Quit processing the current evaluation and move on to the "err process'".
Am i close?
页: 1 [2]
查看完整版本: Question 18 answers go here