"Pseudo" Default VBA Error handling
Codes to mimic VBA Default Error handling.
The code below, using user define VBA error handling, is an approximation to part of the VBA Default Error handling. ( The line numbers are just added to help the further explanations: they are not needed for these codes. )
Run the code.
We can, to a first approximation say that the _ OK _ Button which appears in the pop up message box is doing similar to the _ Beenden/Stop _ Button from Default VBA Error handling.
Code:
' -1 ' No Exception state before the code is run ( Ending a code clears any exception )
Sub PseudoVBADefaultErrorhandlingPartial()
0
1 On Error GoTo ErrHndler ' '_- Hang hook on chain of events that signify to go to call back procedure when chain is waggled by error erection
Dim Db As Double
' Coding
Let Db = 1 / 0 ' '_- When the error occurs, the Exception software has the code from ErrHndler included in it
' Other Coding. In the case of an error this will never be done
GoTo AfterErrHndler
ErrHndler: ' =========================== ' Call back procedure. Code to be hooked on to Exception software. User defined pseudo VBA Error handling
MsgBox prompt:="Laufzeitfehler '" & Err.Number & "':" & vbCr & vbLf & Err.Description & " ", Title:="Microsoft Visual Basic": Debug.Print "Laufzeitfehler '" & Err.Number & "':" & vbCrLf & Err.Description & " " ' From VB Editor , hit Ctrl+g to display the Immediate Window --- Laufzeitfehler '11': Division durch Null Runtime Error '11': division with zero
'
Exit Sub ' This will clear the exception state. No exception state once code is Ended
AfterErrHndler: '=======================
'You never get here
' Other Coding ' In the case of an error this will never be done
End Sub ' This will clear the exception state. No exception state once code is Ended
' -1
Sub PseudoVBADefaultErrorhandlingPartialSimplified()
On Error GoTo ErrHndler '
Dim Db As Double
' Coding
Let Db = 1 / 0 ' When the error occurs, the Exception software has the code from ErrHndler included in it
' Other Coding. In the case of an error this will never be done
Exit Sub
ErrHndler: ' Error handling code section
MsgBox prompt:="Laufzeitfehler '" & Err.Number & "':" & vbCr & vbLf & Err.Description & ""
End Sub
Error handling code section
Just to clarify what I am doing here: I am using error handling techniques to make an error handler similar to the default VBA error handler. This is just by way of a learning exercise to get familiar with VBA error situations in general.
The code section as indicated within ==== is typically referred to as the Error handling code section. In this case the code carried out as part of the Exception Coding is within that section. But it need not be. That referral can be meant to include code lines such as GoTo LabelOrLineNumber or also the execution of any of the three resumes, which effectively bring the normal code back into action. So the term error handling code section is not clearly defined.
To a first approximation the error handling code section is the code section as indicated within ====
The error handling code section need not be , but often is, at the end of the main code. In either case it is necessary to organise that in normal code progressing when no error occurs, that error handling code section will be bypassed.
If, in the last code the error handling code section were at the end, then the label AfterErrHndler: could be omitted, and the Goto AfterErrHndler replaced with Exit Sub
Here the same code again in a more typically seen simplified form
Code:
' -1
Sub PseudoVBADefaultErrorhandlingPartialSimplified() ' 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=10551&viewfull=1#post10551
On Error GoTo ErrHndler '
Dim Db As Double
' Coding
Let Db = 1 / 0 ' When the error occurs, the Exception software has the code from ErrHndler included in it
' Other Coding. In the case of an error this will never be done
Exit Sub
ErrHndler: ' Error handling code section
MsgBox prompt:="Laufzeitfehler '" & Err.Number & "':" & vbCr & vbLf & Err.Description & ""
End Sub
Bookmarks