What To Do When Something Goes Wrong: Handling Errors

In this lesson, you will learn how to create basic error-handling code for your programs.

Even the best designed programs sometimes encounter errors. Some errors are defects in your code that can be found and corrected. Other errors are a natural consequence of the program; for example, your program might attempt to open a file that is already in use. In cases like this, errors can be predicted but not prevented. As a programmer, it is your job to predict these errors and help your program deal with them.

Run-Time Errors

An error that occurs while a program is running is called a run-time error. A run-time error occurs when a program tries to do something it wasn't designed to do. For example, if your program attempts to perform an invalid operation, such as converting a non-numeric string to a numeric value, a run-time error occurs.

When a run-time error occurs, the program issues an exception, which deals with errors by looking for code within the program to handle the error. If no such code is found, the program stops and has to be restarted. Because this can lead to the loss of data, it is wise to create error-handling code wherever you anticipate errors occurring.

The Try...Catch...Finally Block

You can use the Try...Catch...Finally block to handle run-time errors in your code. You can Try a segment of code—if an exception is issued by that code, it jumps to the Catch block, and then the code in the Catch block is executed. After that code has finished, any code in the Finally block is executed. The entire Try...Catch...Finally block is closed by an End Try statement. The following example illustrates how each block is used.

  ' Code here attempts to do something. 
  ' If an error occurs, code here will run. 
  ' Code in this block will always run. 
End Try

First, the code in the Try block is executed. If it runs without error, the program skips the Catch block and runs the code in the Finally block. If an error does occur in the Try block, execution immediately jumps to the Catch block, and the code there is run; then the code in the Finally block is run.

Try It!

To use the Try...Catch block

  1. On the File menu, choose New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Application.

  3. In the Name box, type TryCatch and then click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag one TextBox control and one Button control onto the form.

  5. Double-click the Button to open the Code Editor.

  6. In the Button1_Click event handler, type the following code:

      Dim aNumber As Double = CDbl(Textbox1.Text)
      MsgBox("You entered the number " & aNumber)
      MsgBox("Please enter a number.")
      MsgBox("Why not try it again?")
    End Try
  7. Press F5 to run the program.

  8. In the text box, type a numeric value and click the button. A message box that displays the number you entered, followed by an invitation to try again, is displayed.

  9. Next, type a non-numeric value in the text box, such as a word, and click the button. This time, when the program attempts to convert the text in the text box to a number, it cannot, and an error occurs. Instead of finishing the code in the Try block, the Catch block is executed and you receive a message box asking you to enter a number. The Finally block then executes, inviting you to try again.

Next Steps

In this lesson, you learned how to use Try...Catch...Finally blocks to create a basic error-handling structure. This lesson ends the introduction to the Visual Basic language. In the next series of lessons, you will learn how to create the user interface for your programs.

Next Lesson: Creating the Visual Look of Your Program: Introduction to Windows Forms

See Also


Making a Program Choose Between Two Possibilities: The If...Then Statement

Know Your Bugs: Three Kinds of Programming Errors


Try...Catch...Finally Statement (Visual Basic)

Other Resources

Structured Exception Handling in Visual Basic