Instead of in each sub adding error handling, is there a way to have a form wide error handling?
Printable View
Instead of in each sub adding error handling, is there a way to have a form wide error handling?
The easy answer is No, but there may be a GLOBAL error handler, but each module has to RAISE and event and in turn call it. I've never done it, though.
I use error control for each module that I anticipate may produce an error.
hmm..not what i wanted to hear : /...thanks
Even though there is an outdated On Local Error... statement which implies there might be a global error handler, there isn't. If you want to put the same error handling, or an error handling outline, in several subs you can use MZTools which will do it at the press of a button for each sub.
yeah i know that, but i was hoping i could put it at the top of my form or something under option explicit "On Private Error goto ErrHndler" or something of the sort..That would be a neat feature
Here is an example of what we use...
VB Code:
Private Sub PopulateBL() '<EhHeader> On Error GoTo PopulateBL_Err '</EhHeader> Dim rsRecords As New ADODB.Recordset 100 With rsRecords 102 .Open "SELECT HBL_BLNumber FROM HBL WHERE HBL_Status <> 3", cnn, adOpenDynamic, adLockOptimistic 104 Do While Not .EOF 106 cboBL.AddItem .Fields("HBL_BLNumber") 108 .MoveNext Loop 110 cboBL.AddItem vbNullString 112 .Close End With 114 Set rsRecords = Nothing '<EhFooter> Exit Sub PopulateBL_Err: Select Case ErrorGlobalHandler("AISIS.frmHBL.PopulateBL") Case vbAbort Exit Sub Case vbRetry Resume Case vbIgnore Resume Next Case Else MsgBox Err.Description, vbCritical, "Critical Error Encountered" End End Select '</EhFooter> End Sub
VB Code:
Public Function ErrorGlobalHandler(ByVal strErrorSource As String) As Long Dim strErrorMessage As String strErrorMessage = "Error Occured In: " & strErrorSource & vbCrLf & _ "Error Date & Time: " & Now & vbCrLf & _ "Error Description: " & Err.Description & vbCrLf & _ "Error Number: " & Err.Number & vbCrLf & _ "Line Number: " & Erl & vbCrLf 'Pass to other subroutine to trap any error that might occur in the ErrorGlobalHandler ErrorGlobalHandler = PromptAndLogError(strErrorMessage, App.Path) End FunctionVB Code:
Public Function PromptAndLogError(ByVal ErrorMessage As String, ByVal SystemPath As String, Optional ShowPrompt As Boolean = True) As Long On Error Resume Next Dim fso As FileSystemObject Dim tsm As TextStream Dim ErrorFile As String Set fso = New FileSystemObject ErrorFile = SystemPath & "\ErrorLog" & Format$(Date, "mmddyyyy") & ".txt" If fso.FileExists(ErrorFile) = True Then Set tsm = fso.OpenTextFile(ErrorFile, ForAppending) tsm.Write String$(30, "-") & vbCrLf & ErrorMessage Else Set tsm = fso.CreateTextFile(ErrorFile, True) tsm.Write ErrorMessage End If tsm.Close Set tsm = Nothing Set fso = Nothing If ShowPrompt = True Then PromptAndLogError = MsgBox(ErrorMessage & vbCrLf & "Please report to the programmer any persistent error. ", vbCritical + vbAbortRetryIgnore, "Error Encountered") End If End Function
Dee-u, that's still not the same as a global error handler. You still need to put the actual error handling code in each sub. The fact that you then call a public function that shows a standard error message and logs the error doesn't really make it a global error handler.
Yap, I know, but I hoped it might be of some help or sort....
thanks dee-u, but i was looking for a way around that : /
that is what i was talking about in #2. I knew it existed.
If an error is not handled, it is passed back to the calling procedure.
This means that only the first procedure in the call stack really needs to have an error handler. Basically this is every event handler.
That's correct Frans, but we don't write our own message pump in VB since we simply rely on VB to do that for us. That means we just write code in our event procedures and they are magically just raised... So we have no control over the call stack either. But letting an error traverse all the way up the call stack is also a bad programming practice, and I bet that if VB would have had some kind of Form wide error handling that Remix asked for there would be hundreds of books and articles that would warn us from using that bad bad practice.
Errors should be delt with where the error is raised or by the immediate caller.
Yes, I agree.
I prefer to handle errors at the place where they occur.
If I need to pass it to the calling procedure, I raise another error.
But I prefer to code in a way, that most situations that can cause an error are checked before the error occurs.
eg.
VB Code:
If c <> 0 Then a = b/c Else MsgBox "Moron, you can't divide by zero" End If