# Visual Basic > Visual Basic FAQs >  Classic VB - Why do errors crash my program, and how can I stop that from happening?

## si_the_geek

*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:
Private Sub Command1_Click() Dim x As Integer  x = CInt(Text1.Text)    'this line will cause an error if the text is not a number  MsgBox x 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:
Private Sub Command1_Click() '** Turn on error handling  On Error GoTo ErrorHandler: '** code as beforeDim x As Integer  x = CInt(Text1.Text)    'this line will cause an error if the text is not a number  MsgBox x '** The error handlerExit Sub  '(this stops the error handler from running when there is no error!)ErrorHandler:  MsgBox "An error occured -  error  " & Err.Number & ": " & Err.Description 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:
...'** The error handlerExit Sub          ErrorHandler:    'code to deal with errors we think are likely  If Err.Number = 13 Then   '(Error 13 is Type mismatch - the text cannot be converted to a number)    Text1.Text = "1"   'automatic fix - put a number into the textbox        Resume             'retry the line that had the error  Else     'code to deal with any unexpected errors    MsgBox "An error occured -  error  " & Err.Number & ": " & Err.Description   End If 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).

----------

