Results 1 to 10 of 52

Thread: Resume On Error GoTo 0 -1 GoTo Error Handling Statements Runtime VBA Err Handling ORNeRe GoRoT N0Nula 1

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10

    Error Handling statements Resumes .. Error Handling statement Resume

    Error Handling statements Resumes
    Error Handling statement Resume


    Pseodo Resume Code
    Below is an attempt a code that does what the actual Resume does
    In the code are two lines which error.
    At each error line the exception is raised and the code becomes part of the exception software running from GetMilkLuv:

    As Resume takes us back to where the error occurred to, as it were, "try again" , then usually some attempt in the error handling would be done to prevent the error occurring again. ( That does not have to be the case: If one was expecting something external to occur which might prevent the code line erroring, then a resume without doing anything would be an option. However this is a very unadvisable use of Resume as it has the potential for causing an infinite looping if nothing prevents the error continuingly occurring. So the fist thing done at the error handler is giving a value to the be used in the denominator other than 0, so that 10 / TNominator no longer gives us an error.
    The line of the error then needs to be noted. We use for the first time here a method, Erl(). This is not clearly defined in any documentation. I expect this is some method used internally as needed, from within the Exception State, to return the last executed line in the “real world”/ normal coding before the error. It has therefore become known as a method or function to return the line that errored.
    This cannot be done after the next line, On Error GoTo -1 , as On Error GoTo -1 appears, in addition to its main purpose of clearing the exception, to additionally prevent the Erl function from giving us the line number of the last error.
    Note that On Error GoTo -1 has also removed the information in the Err object about the last error. Hence code line 55 gives us no information.
    Note that On Error GoTo -1 does not do the action of On Error GoTo 0. That is to say, the defined error handler is still "switched on" , or "pluged in and ready to be tripped by an error" as it were. One could say that it is deactivated. But it has not been "unplugged". Possibly you could think of it as the "trap being reset".
    The last part of the error handler is to determine where to go back to. It is quite messy and requires the use of line numbers so demonstrates one good reason for having a predefined Resume.
    Code:
    Sub PseudoResumeGoToGet5ButComeBackDarling()    '     https://excelfox.com/forum/showthread.php/2239-Resume-On-Error-GoTo-0-1-GoTo-Error-Handling-Statements-Runtime-VBA-Err-Handling-ORNeRe-GoRoT-N0Nula-1?p=10556&viewfull=1#post10556
    10    On Error GoTo GetMilkLuv
    20   Dim TNominator As Long, RslTwat As Long
    30   ' Other Code
    40    Let TNominator = 0
    50    Let RslTwat = 10 / TNominator
    55 MsgBox Err.Description ' This gives blank. On Erro GoTo -1 has cleared the Err object of infomation
    60   ' other code
    70    Let TNominator = 0
    80    Let RslTwat = 10 / TNominator
    90   ' 0ther code
    100  Exit Sub
    110 GetMilkLuv:  ' "Error handling Code section" is from here until the End
    120   Let TNominator = 5 ' get 5 to take back with you
    130  Dim errLine As Long: Let errLine = Erl ' this must be done before On Error GoTo -1 , as that clears the recorded error line
    140   On Error GoTo -1
    141 ' Err.Clear ' I do not need to do this, as it is effectively done as part of On Error GoTo -1  Note: Err.Clear removes the infomation, if an is present, in the Err object. it has no efffect on the actual error state
    145  MsgBox prompt:="We want to go back to the erroring line " & errLine & " and try again"
    150     If errLine = 10 Then
        GoTo 10:
        ElseIf errLine = 20 Then
        GoTo 20
        ElseIf errLine = 30 Then
        GoTo 30
        ElseIf errLine = 40 Then
        GoTo 40
        ElseIf errLine = 50 Then
        GoTo 50
        ElseIf errLine = 60 Then
        GoTo 60
        ElseIf errLine = 70 Then
        GoTo 70
        ElseIf errLine = 80 Then
        GoTo 80
        ElseIf errLine = 90 Then
        GoTo 90
        ElseIf errLine = 100 Then
        GoTo 100
        ElseIf errLine = 110 Then
        GoTo 110
        ElseIf errLine = 120 Then
        GoTo 120
        ElseIf errLine = 130 Then
        GoTo 130
        ElseIf errLine = 140 Then
        GoTo 140
        ElseIf errLine = 150 Then
        GoTo 150
        End If
    End Sub
    '
    The equivalent code using the VBA Resume statement is shown below:
    Code:
    Sub VBAResume()
     On Error GoTo GetMilkLuv
    Dim TNominator As Long, RslTwat As Long
    ' Other Code
     Let TNominator = 0
     Let RslTwat = 10 / TNominator
     MsgBox Err.Description ' This gives blank. On Erro GoTo -1 has cleared the Err object of infomation
    ' Other code
     Let TNominator = 0
     Let RslTwat = 10 / TNominator
    ' 0ther code
    Exit Sub
    GetMilkLuv:  ' "Error handling Code section" is from here until the End
     Let TNominator = 5 ' get 5 to take back with you
     MsgBox prompt:="We want to go back to the erroring line and try again"
     Resume
    End Sub
    '
    Last edited by DocAElstein; 03-23-2023 at 03:26 AM.

Similar Threads

  1. Replies: 8
    Last Post: 09-01-2015, 01:50 AM
  2. Difference Between 'On Error GoTo 0' And 'On Error GoTo -1'
    By Transformer in forum Familiar with Commands and Formulas
    Replies: 7
    Last Post: 07-02-2015, 04:07 PM
  3. Replies: 2
    Last Post: 05-14-2013, 01:02 AM
  4. Runtime Error 481 invalid figure when PNG
    By Tony in forum Excel Help
    Replies: 0
    Last Post: 02-12-2013, 12:59 AM
  5. Replies: 10
    Last Post: 04-07-2012, 05:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •