Library tutorials & articles
Error Handling
Introduction
When you create an application, there are certain unknown elements. For example, if the users tries to save the current file which is on a floppy, and the floppy is not inserted when your application attempts to access it, your application will crash and you would lose any unsaved information. Error handling intercepts these errors, so you can give the user a useful error message, instead of your application crashing. i.e. instead of getting a message "Err 45 Disk not ready" and then the application crashing, you could change your code so that the user would get the message "Please insert a floppy disk into Drive A", and then give the user an option to retry or cancel.
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
Sometimes, I use a module to take care of error handling:
Sub ErrHandl(Error As ErrObject)
Dim S As String
Dim D As Date
Dim TimeS As String
D = Now
TimeS = "[" & FormatDateTime(D, vbShortDate) & " " & _
FormatDateTime(D, vbLongTime) & "]"
S = "Error occured. " & Error.Number & ": " & _
Error.Description & _
" (" & Error.Source & ")"
Debug.Print TimeS & " " & S
End Sub
And then, in my form:
On Error GoTo ErrHandl:
-- Code --
Exit Sub
ErrHandl:
ErrHandl Err
End Sub
Of course, the module can be optimized, but I don't make biig applications, so I just use Debug.Print
Most people don't notice this, but if you have error-handling code at the end of a sub/function, you have to place Exit Sub or Exit Function before it, or it'll execute the error handler afterward even though nothing went wrong.
Resume Next will execute the next statement, even if it's inside an IF statement. Worse of all, VB/VBscript evaluates the whole expression.
Public Function test2()
On Error Resume Next
If (1 = 2) And ((1 / 0) = 5) Then
MsgBox "Why?"
Else
MsgBox "Ok"
End If
End Function
This thread is for discussions of Error Handling.