2

In VBA error handling is done by on error statement.

I want to temporarily change the error handling and then go back to the previous behavior afterward. How would it be possible to check the current error handling and store it in a variable (I couldn't find anything in the references)?

'set the error handling to s.th. "on error... "
'some code with the regular error handling

'change the error handling to "on error ..." (regardless of what it was before)
'some code with the new error handling

'change back to the previous error handling
'some code with the regular error handling

Background: I needed to do a is nothing check on a Variant array to exclude empty object indexes from being used, but is nothing applied to an array index that holds a value throws an exception, so I temporary wanted to change the error handling to on error resume next. Eventually is solved this using a different approach but I'm still wondering if I can determine the current error handling somehow during runtime Here's the question and answer to my original problem.

EDIT: I know I can check my previous code manually to find out what type of error handling has been used. However I want to avoid that (to save time).

I suppose as a workaround I could set an additional variable with the state which I can then check for the current state, although this will result in quite a bit of overhead. Something like this:

Dim errorHandling as String

errorHandling = "resumeNext"
on error resume next
'some code

'changing the error handling temp.
'some other code

'changing the error handling to it's previous state
if errorhandling = "resumeNext" then
   On Error Resume Next
elseif errorhandling = "GoToErrorhandler" then
   On Error GoTo errorhandler
End If

'Rest of the code
Albin
  • 1,000
  • 1
  • 11
  • 33
  • You need to add the code to this question. Also, at least, add how you are filling the array. When you declare an array `As Variant`, each of the elements is assigned the value `Empty`, not `Nothing`, hence you would need to do something like `If IsEmpty(MyArray(i)) Then` or `If Not IsEmpty(MyArray(i)) Then`. Why don't you share the complete code so you could see how this is applied. A rule of thumb would be that using error trapping (handling) is used as 'the last resort' i.e. when there is no other (easy) way. – VBasic2008 Jun 06 '22 at 11:26
  • @VBasic2008 this question is not about the array, I just want to temporarily change the error handling (without having to check manually what it was before). – Albin Jun 06 '22 at 12:18
  • Why don't you explain what `temporarily change the error handling` means? Without an explanation, it doesn't mean a thing to me. Maybe add some code or pseudo-code to your post. – VBasic2008 Jun 06 '22 at 12:23
  • Sorry I wasn't quick enough with my editing... – Albin Jun 06 '22 at 12:30

1 Answers1

1

Read/Write to Array

Option Explicit

Sub ReadWriteArrayExample()

    Dim myArray() As Variant: ReDim myArray(1 To 10)
    
    Dim i As Long
    Dim n As Long
    
    ' Fill the array.
    For i = 1 To 10
        n = Application.RandBetween(0, 1)
        If n = 1 Then ' write a random number between 1 and 10 inclusive
            myArray(i) = Application.RandBetween(1, 10)
        'Else ' "n = 0"; leave the element as-is i.e. 'Empty';do nothing
        End If
    Next i
    
    ' Debug.Print the result.
    Debug.Print "Position", "Value"
    For i = 1 To 10
        If Not IsEmpty(myArray(i)) Then ' write the index and the value
            Debug.Print i, myArray(i)
        'Else ' is empty; do nothing
        End If
    Next i

End Sub

Error Handling

Sub ErrorHandling()

    Const ProcName As String = "ErrorHandling"
    On Error GoTo ClearError ' enable error trapping

    ' Some code

    On Error Resume Next ' defer error trapping
        ' Some tricky code
    On Error GoTo ClearError ' re-enable error trapping

    ' Some Code

ProcExit:
    Exit Sub
ClearError:
    Debug.Print "'" & ProcName & "' Run-time error '" _
        & Err.Number & "':" & vbLf & "    " & Err.Description
    Resume ProcExit
    
End Sub
VBasic2008
  • 44,888
  • 5
  • 17
  • 28
  • that only checks if the whole array is empty if I'm not mistaken?! – Albin Jun 06 '22 at 12:14
  • You can safely run (it only writes to the Immediate window) the first code and see what it does. Run it several times to see the difference. – VBasic2008 Jun 06 '22 at 12:34
  • Thanks for your second suggestion (error code handling), unfortunately, it doesn't give me the desired result. `On Error GoTo ClearError ' re-enable error trapping` only works if the previous error handling was set to `On Error GoTo ClearError`. I want to avoid that and have a more dynamic approach where I don't have to manually keep track of the error handling. I possibly wasn't clear enough in my question, sorry about that. – Albin Jun 10 '22 at 06:09
  • Regarding the suggestion for the array, it still gives me an error: isempty() won't catch empty objects (`Set myArray(1) = Nothing`) leading to an exception on the debug.print line. But I solved this problem already using isobject() (see my other question). But thanks for your suggestion anyway. – Albin Jun 10 '22 at 06:36