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

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
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: 17750
  • Have thousands of dwgs to process? Contact me.
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.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

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 ..
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
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

;)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

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 »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Question 18 answers go here
« Reply #5 on: August 25, 2005, 09:24:18 PM »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • 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

Find me on https://parler.com @kblackie

Jürg Menzi

  • Swamp Rat
  • Posts: 599
  • 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 A2k16... A2k24 - 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: 599
  • 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 A2k16... A2k24 - Start R2.18

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • 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

Find me on https://parler.com @kblackie

Jürg Menzi

  • Swamp Rat
  • Posts: 599
  • 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 A2k16... A2k24 - 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...

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
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?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org