Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 11

Handling Errors in Code - Trappable Errors

In parts of your applications, you can anticipate the types of errors that can occur. If your application is reading and writing to disk files, for example, you know that you may encounter some file errors. Disks can be full, the user may not have placed a disk or CD-ROM in a drive, or a disk could be unformatted or corrupt.

Errors that you can anticipate and code for in your applications can be found in Visual Basic's Help file. If you search Help for "Trappable Errors," you will get a list of errors for which you can trap in your application (see Figure 11.3).

Visual Basic's Help for Trappable Errors.
FIGURE 11.3 Visual Basic's Help for Trappable Errors.

Help will provide you with a description of the error and the number corresponding to that error. These will be the same number and description found in the Err.Number and Err.Description properties after the runtime error has occurred. If you select an error message from Help—as shown in Figure 11.3—you will get further information for that error, as shown in Figure 11.4.

Detailed Help for Visual Basic errors.
FIGURE 11.4 Detailed Help for Visual Basic errors.

It makes sense to anticipate and code for errors that could occur frequently in your application. If you know where errors may occur and what those errors are, you can handle them gracefully.

Instead of displaying an error message and ending your application when the user has not placed a disk in a drive, you may want to give the user a chance to use a disk or cancel what he or she was trying to do. You can trap for error number 71, Disk not ready, and prompt the user for his or her input. As part of your error-handling routine, you might include the following:

If Err.Number = 71 Then ' Disk Not Ready
   If Msgbox("Disk not ready. Retry?", vbYesNo) = vbYes Then
      Resume
   Else
      Exit Sub
   End if
End if

Now the user has a choice. If he or she places a disk in the drive and clicks on the Yes button, the process can continue. If the No button is clicked, Visual Basic will exit the sub-routine.

If a disk has not been formatted, you may want to give the user the option to format that disk. If a disk is full, you can let the user insert a new disk. Always be aware of the problems that can occur, especially because of user interaction, and code accordingly.


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved