TheSwamp
Code Red => VB(A) => Topic started by: Mark on August 25, 2005, 08:07:38 PM
-
In Visual Basic, what does the line "On Error Resume Next" instruct the program to do?
-
... to allow the programmer to employ poor coding habits.
Just kidding, sometime that's a perfectly valid route to go.
:)
-
I thought it was to repair the damage done from having to type the word Dim a coupla hundred times a day.
I'll probably get dropped under a camel for that ..
-
I thought it was to repair the damage done from having to type the word Dim a coupla hundred times a day.
Dim filehandle As Integer, _
nullheader As String * 7, _
stream As String, _
rawRecord As RawRecordType, _
i As Long
;)
-
(http://www.theswamp.org/screens/kerry/Michaels%20ScoreCard.png)
-
(http://www.theswamp.org/screens/mp/humilitymodule.png)
-
... to allow the programmer to employ poor coding habits.
In most cases that is indeed the reason ...
However ... it can actually be a very handy tool if used properly ...
Consider a loop that does series of tasks (or even a single task) on a group of objects. We are all familiar with the "For Next" loops ...or at least most of us are ...
It is my understanding (and I am frequently wrong) that this error handler allows you to skip over the items causing this error within the loop ...
example
CharCode = 32
On Error Resume Next
For X = -127 to 127
MsgBox CharCode / X
Next X
Now while the point of this exercise might be pointless, it will catch the divide by zero error that will be generated when X = 0, thus the loop will continue to execute until it is finished.
Now for an example of how it can be used for the lazy programmer ...
Set ThisSSet = ThisDrawing.SelectionSets.Add("NewSSet")
ThisSSet.SelectOnScreen
On Error Resume Next
For Each ACBlock in ThisSSet
MsgBox ACBlock.Name & vbCr & _
"X:" & ACBlock.XScaleFactor & vbCr & _
"Y:" & ACBlock.YScaleFactor & vbCr & _
"Z:" & ACBlock.ZScaleFactor
Next ACBlock
Now what happens is if there is an object that is not a block it will generate an error because of the properties being read and the error handler will do it's job. This is an example of lazy programming as the user could have programmed in a filter to allow for only the selection of blocks. Plus what happens when the function encounters a block that has had the name stripped from the drawing database as in some of the older CadLock drawings.
BUT
If you call another subroutine without an error handler AFTER you use the Resume Next handler, if the called function has an error it will be a pain in the butt to find it because the function will work as though nothing is wrong ... been there done that ... learned the hard way :)
-
Specifies that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred where execution continues... Sample:
Public Function MeSelectionSet(SetNme As String) As AcadSelectionSet
On Error Resume Next
With ThisDrawing.SelectionSets
Set MeSelectionSet = .Add(SetNme)
If Err.Number <> 0 Then
Err.Clear
.Item(SetNme).Delete
Set MeSelectionSet = .Add(SetNme)
End If
End With
End Function
-
on error resume next is error avoidance not error handling...
try
catch ex as exception
finally 'optional
end try
is error handling.
-
try
catch ex as exception
finally 'optional
end try
My guess C++...
-
Actually while it is a common method in C++ it is also available in VB ..
-
Actually while it is a common method in C++ it is also available in VB ..
I'm not a VB guru, anyway thanks for the hint.
-
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
-
Actually while it is a common method in C++ it is also available in VB ..
I'm not a VB guru, anyway thanks for the hint.
resistance is futile, you will be assimilated...
-
> 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?
-
It reads as if straight from a book and would have been nice if it mentioned the fact that it does clear the error or deal with it in any other way but The most firstest winner is Jürg Menzi for:
Specifies that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred where execution continues...
-
It reads as if straight from a book and would have been nice if it mentioned the fact that it does clear the error or ...
Does not.
:P
-
"Does" was supposed to be "doesn't". Really.
-
Nodding.
-
Actually it reads almost EXACTLY like the book ...
-
Actually it reads almost EXACTLY like the book ...
Nodding.
-
resistance is futile, you will be assimilated...
Ok Howi, I give up...:lol:Actually it reads almost EXACTLY like the book ...
EXACTLY like the help file would I say... :)
As you know, my English is a little wobbly and that's the reason that I use sometimes 'prefabricated' phrases. In the actual case I think it's legitimate to use the help statement because it's exactly what I know about 'On Error Resume Next'.
It reads as if straight from a book and would have been nice if it mentioned the fact that it does clear the error or ...
Does not.
This fact is part of my sample:
You've to clear the Err object to avoid unexpected results.
Note:
End Sub, End Function, Exit Sub, Exit Function, Exit Property or Resume Next clears the Err object also...Nodding.
Michael, could that be the reason for your headache? :cheesy:
-
GentlyShakingHeadLeftAndRightToIndicateNoButLaughingAtTheSuggestion.