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.

Comments

  1. 07 Jan 2009 at 23:15
    For Error Handling in Asp.net you can read following article http://dotnetguts.blogspot.com/2007/10/error-handling-in-net-with-example.html
  2. 26 May 2004 at 05:25

    Sometimes, I use a module to take care of error handling:


    Code:
    Option Explicit


    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:


    Code:
    Sub xxx()
       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

  3. 27 Jul 2003 at 23:41

    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.

  4. 10 Apr 2002 at 19:28

    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


  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Error Handling.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related discussion

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...

Want to stay in touch with what's going on? Follow us on twitter!