Results 1 to 1 of 1

Thread: Classic VB - Why do errors crash my program, and how can I stop that from happening?

  1. #1

    Thread Starter
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Classic VB - Why do errors crash my program, and how can I stop that from happening?

    Why do errors make your program crash?
    When an error happens something has gone wrong in your program, so it isn't going to work correctly.. it is also likely that code after the initial error will have errors too (especially if the first error occurred when setting a variable). Depending on what that section of your program does, you could end up doing some serious damage (such as deleting the wrong file).

    Because of reasons like this, VB does something that is annoying but quite sensible.. it prevents any further errors from happening (or more damage being done) by stopping the program.

    While it would be nice for VB to fix the issues for us, it can't.. it is a hard enough task to convert your code into a format that the computer itself can run, actually understanding what a section of code is supposed to do (let alone how to correct any mistakes in it) is even harder - many people have problems doing it, so writing a program (which is all that VB is!) to do it would be a monumental task!

    Apart from fixing any code mistakes you have made, the only way to stop the crashing is to somehow deal with the errors yourself - a task known as Error Handling.


    So how can I handle errors?
    This is a large topic, and there are several methods - none of which cover all situations. Thankfully within VB's help are articles that explain the methods you can use, and situations where you should use them.

    Most of these are in a section called "Debugging Your Code and Handling Errors". The easiest way to find this is to open VB's help and use the Search tab (it helps if you un-tick "match similar words", and tick "search titles only"). You don't have to read all the articles in this section to work out what to do, but the first two are very useful indeed!

    Another useful article is called "When Things Go Wrong: Interacting with Users". Again a Search of the help is the easiest way to find it.

    Online versions of these can currently be found here ('debugging') and here ('interacting'), but are likely to be moved/removed in future (if so, please tell me so I can update these links!).


    Examples
    Here are a couple of examples of how error handling code can be done, but as explained by the articles listed above it is certainly not the only method!

    These are based on a simple example program: the user enters a number into the textbox Text1 and presses the button Command1, the code then displays the integer part of the number.

    This is not great code, as you should actually check that the data the user entered is valid before using it (possibly checking the length of the text, and using IsNumeric), but works as a simple example of how you can add error handling!

    Here is the code without an error handler, if anything other than a number is entered then a message will be shown, and the program will crash:
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim x As Integer
    4.   x = CInt(Text1.Text)    'this line will cause an error if the text is not a number
    5.   MsgBox x
    6.  
    7. End Sub
    We can easily add a very basic error handler to this, which simply shows our own error message (and avoids the crash):
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. '** Turn on error handling
    4.   On Error GoTo ErrorHandler:
    5.  
    6. '** code as before
    7. Dim x As Integer
    8.   x = CInt(Text1.Text)    'this line will cause an error if the text is not a number
    9.   MsgBox x
    10.  
    11. '** The error handler
    12. Exit Sub  '(this stops the error handler from running when there is no error!)
    13. ErrorHandler:
    14.   MsgBox "An error occured -  error  " & Err.Number & ": " & Err.Description
    15.  
    16. End Sub
    As the code is fairly simple and we know what error to expect, we can even expand the error handler to automatically correct the error:
    VB Code:
    1. ...
    2. '** The error handler
    3. Exit Sub          
    4. ErrorHandler:
    5.     'code to deal with errors we think are likely
    6.   If Err.Number = 13 Then   '(Error 13 is Type mismatch - the text cannot be converted to a number)
    7.     Text1.Text = "1"   'automatic fix - put a number into the textbox    
    8.     Resume             'retry the line that had the error
    9.   Else
    10.  
    11.     'code to deal with any unexpected errors
    12.     MsgBox "An error occured -  error  " & Err.Number & ": " & Err.Description
    13.  
    14.   End If
    15.  
    16. End Sub
    Note that the way this error is dealt with is only appropriate because that error (13 - Type Mismatch) can only ever happen in one place in this sub.

    If the same error occured elsewhere in the sub, setting the text in the Textbox would not solve it - so the error would happen again, and the error handler would keep trying to run the problematic line!


    Any questions?
    As this is such a large topic, there is a good chance that you will have questions that haven't been answered here.

    If so, see if the other articles in the 'Dealing with Errors' section of Classic VB FAQ answer them. If not, try using the Search feature at the top of the page.

    If you cant find the answers, please post your question as a new thread in the appropriate forum (probably Classic Visual Basic).
    Last edited by si_the_geek; Jul 22nd, 2017 at 05:00 PM. Reason: updated links

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width