Author Topic: Question 18 answers go here  (Read 4170 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28683
Question 18 answers go here
« 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?
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17498
Question 18 answers go here
« Reply #1 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.

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Question 18 answers go here
« Reply #2 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 ..
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

MP

  • Seagull
  • Posts: 17498
Question 18 answers go here
« Reply #3 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

;)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Question 18 answers go here
« Reply #4 on: August 25, 2005, 08:48:59 PM »
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

MP

  • Seagull
  • Posts: 17498
Question 18 answers go here
« Reply #5 on: August 25, 2005, 09:24:18 PM »
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16727
  • Superior Stupidity at its best
Question 18 answers go here
« Reply #6 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 :)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Jürg Menzi

  • Swamp Rat
  • Posts: 597
  • Oberegg, Switzerland
Question 18 answers go here
« Reply #7 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
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k14... A2k18 - Start R2.18

Dirty Howi

  • Guest
Question 18 answers go here
« Reply #8 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.

Jürg Menzi

  • Swamp Rat
  • Posts: 597
  • Oberegg, Switzerland
Question 18 answers go here
« Reply #9 on: August 26, 2005, 11:14:56 AM »
Quote from: Dirty Howi
try
catch ex as exception
finally 'optional
end try
My guess C++...
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k14... A2k18 - Start R2.18

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16727
  • Superior Stupidity at its best
Question 18 answers go here
« Reply #10 on: August 26, 2005, 11:22:43 AM »
Actually while it is a common method in C++ it is also available in VB ..
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Jürg Menzi

  • Swamp Rat
  • Posts: 597
  • Oberegg, Switzerland
Question 18 answers go here
« Reply #11 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.
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k14... A2k18 - Start R2.18

Troy Williams

  • Guest
Question 18 answers go here
« Reply #12 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


Dirty Howi

  • Guest
Question 18 answers go here
« Reply #13 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...

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9285
Question 18 answers go here
« Reply #14 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?
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

Bob Wahr

  • Guest
Question 18 answers go here
« Reply #15 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...

MP

  • Seagull
  • Posts: 17498
Question 18 answers go here
« Reply #16 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
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Bob Wahr

  • Guest
Question 18 answers go here
« Reply #17 on: August 26, 2005, 01:19:29 PM »
"Does" was supposed to be "doesn't".  Really.

MP

  • Seagull
  • Posts: 17498
Question 18 answers go here
« Reply #18 on: August 26, 2005, 01:20:30 PM »
Nodding.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16727
  • Superior Stupidity at its best
Question 18 answers go here
« Reply #19 on: August 26, 2005, 02:15:27 PM »
Actually it reads almost EXACTLY like the book ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

MP

  • Seagull
  • Posts: 17498
Question 18 answers go here
« Reply #20 on: August 26, 2005, 02:23:55 PM »
Quote from: Keith
Actually it reads almost EXACTLY like the book ...

Nodding.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Jürg Menzi

  • Swamp Rat
  • Posts: 597
  • Oberegg, Switzerland
Question 18 answers go here
« Reply #21 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:
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k14... A2k18 - Start R2.18

MP

  • Seagull
  • Posts: 17498
Question 18 answers go here
« Reply #22 on: August 27, 2005, 08:16:57 AM »
GentlyShakingHeadLeftAndRightToIndicateNoButLaughingAtTheSuggestion.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox