Since I see it again and again that unrecoverable errors in procedures are displayed to the user with a MsgBox but then the code continues as if nothing had happened, I put the problem up for discussion in this thread.
For example, this error handling is incorrect:
In this example, no error handling in Func1 would have been better.
How to do it right?
The MsgBox could be replaced by an extra ErrorHandler procedure, but this must allow the error to be passed up to the top level.
Is there interest to create such a (global) error handling together?
However, I would not come immediately with a code proposal, but first work out a concept together.
Note: All who use vbWatchdog do not need this.
Attached is an example file to show the problem. (Hopefully I managed to make it reasonably understandable in the code ).
Of course, the example creates the error artificially. Therefore, please take this as a given and think only about error handling and not about error prevention.
For example, this error handling is incorrect:
Code:
Private Sub MainProc()
Dim x As Long
Dim y As Long
On Error GoTo HandleErr
x = 1
y = Func1(x)
MsgBox "Result: " & y
ExitHere:
Exit Sub
HandleErr:
MsgBox "Error: (" & Err.Source & ") " & Err.Description
Resume ExitHere
End Sub
Public Function Func1(ByVal x As Long) As Long
On Error GoTo HandleErr
Func1 = x * Func2(x)
ExitHere:
Exit Function
HandleErr:
MsgBox "Error: (" & Err.Source & ") " & Err.Description ' <--- WRONG!
Resume ExitHere
End Function
Public Function Func2(ByVal x As Long) As Long
Func2 = x / 0 ' to raise an error
End Function
How to do it right?
The MsgBox could be replaced by an extra ErrorHandler procedure, but this must allow the error to be passed up to the top level.
Is there interest to create such a (global) error handling together?
However, I would not come immediately with a code proposal, but first work out a concept together.
Note: All who use vbWatchdog do not need this.
Attached is an example file to show the problem. (Hopefully I managed to make it reasonably understandable in the code ).
Of course, the example creates the error artificially. Therefore, please take this as a given and think only about error handling and not about error prevention.