TheSwamp

Code Red => VB(A) => Topic started by: Mark on August 25, 2005, 08:07:38 PM

Title: Question 18 answers go here
Post by: Mark on August 25, 2005, 08:07:38 PM
Quote
In Visual Basic, what does the line "On Error Resume Next" instruct the program to do?
Title: Question 18 answers go here
Post by: MP on August 25, 2005, 08:14:33 PM
... to allow the programmer to employ poor coding habits.











Just kidding, sometime that's a perfectly valid route to go.

:)
Title: Question 18 answers go here
Post by: Kerry on August 25, 2005, 08:33:43 PM
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 ..
Title: Question 18 answers go here
Post by: MP on August 25, 2005, 08:39:08 PM
Quote from: Kerry Brown
I thought it was to repair the damage done from having to type the word Dim a coupla hundred times a day.

Code: [Select]
Dim filehandle As Integer, _
    nullheader As String * 7, _
    stream     As String, _
    rawRecord  As RawRecordType, _
    i          As Long

;)
Title: Question 18 answers go here
Post by: Kerry on August 25, 2005, 08:48:59 PM
(http://www.theswamp.org/screens/kerry/Michaels%20ScoreCard.png)
Title: Question 18 answers go here
Post by: MP on August 25, 2005, 09:24:18 PM
(http://www.theswamp.org/screens/mp/humilitymodule.png)
Title: Question 18 answers go here
Post by: Keith™ on August 25, 2005, 09:53:00 PM
Quote from: MP
... 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
Code: [Select]

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 ...

Code: [Select]

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 :)
Title: Question 18 answers go here
Post by: Jürg Menzi on August 26, 2005, 05:08:23 AM
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:
Code: [Select]
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
Title: Question 18 answers go here
Post by: Dirty Howi on August 26, 2005, 10:51:29 AM
on error resume next is error avoidance not error handling...

try

catch ex as exception

finally 'optional

end try

is error handling.
Title: Question 18 answers go here
Post by: Jürg Menzi on August 26, 2005, 11:14:56 AM
Quote from: Dirty Howi
try
catch ex as exception
finally 'optional
end try
My guess C++...
Title: Question 18 answers go here
Post by: Keith™ on August 26, 2005, 11:22:43 AM
Actually while it is a common method in C++ it is also available in VB ..
Title: Question 18 answers go here
Post by: Jürg Menzi on August 26, 2005, 11:35:52 AM
Quote from: Keith
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.
Title: Question 18 answers go here
Post by: Troy Williams on August 26, 2005, 11:45:54 AM
Here is an example of a useful on error resume next:

Code: [Select]

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:
Code: [Select]

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:

Code: [Select]

'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

Title: Question 18 answers go here
Post by: Dirty Howi on August 26, 2005, 11:59:17 AM
Quote from: Jürg Menzi
Quote from: Keith
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...
Title: Question 18 answers go here
Post by: JohnK on August 26, 2005, 12:53:48 PM
> 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?
Title: Question 18 answers go here
Post by: Bob Wahr on August 26, 2005, 01:14:06 PM
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:
Quote from: Jürg Menzi
Specifies that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred where execution continues...
Title: Question 18 answers go here
Post by: MP on August 26, 2005, 01:16:29 PM
Quote from: Bob Wahr
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
Title: Question 18 answers go here
Post by: Bob Wahr on August 26, 2005, 01:19:29 PM
"Does" was supposed to be "doesn't".  Really.
Title: Question 18 answers go here
Post by: MP on August 26, 2005, 01:20:30 PM
Nodding.
Title: Question 18 answers go here
Post by: Keith™ on August 26, 2005, 02:15:27 PM
Actually it reads almost EXACTLY like the book ...
Title: Question 18 answers go here
Post by: MP on August 26, 2005, 02:23:55 PM
Quote from: Keith
Actually it reads almost EXACTLY like the book ...

Nodding.
Title: Question 18 answers go here
Post by: Jürg Menzi on August 27, 2005, 07:19:29 AM
Quote from: Dirty Howi
resistance is futile, you will be assimilated...
Ok Howi, I give up...:lol:
Quote from: Keith
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'.
Quote from: MP
Quote from: Bob Wahr

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...
Quote from: MP
Nodding.
Michael, could that be the reason for your headache? :cheesy:
Title: Question 18 answers go here
Post by: MP on August 27, 2005, 08:16:57 AM
GentlyShakingHeadLeftAndRightToIndicateNoButLaughingAtTheSuggestion.