Writing a Disc Drive Error Handler


Writing a Disc Drive Error Handler

The problem with the Disc Drive Error program isn't that it somehow defies the inherent capabilities of Visual Basic to process errors. We just haven't specified what Visual Basic should do when it encounters an exception that it doesn't know how to handle. The solution to this problem is to write a Try…Catch code block that recognizes the error and tells Visual Basic what to do about it. You'll add this error handler now.

Use Try…Catch to trap the error

  1. Display the Button1_Click event procedure if it isn't visible in the Code Editor.

    You need to add an error handler to the event procedure that's causing the problems. As you'll see in this example, you actually build the Try…Catch code block around the code that's the potential source of trouble, protecting the rest of the program from the run-time errors it might produce.

  2. Modify the event procedure so that the existing FromFile statement fits between Try and Catch statements, as shown in the following code block:

    Try     PictureBox1.Image = _       System.Drawing.Bitmap.FromFile("d:\fileopen.bmp") Catch     MsgBox("Please insert the disc in drive D!") End Try

    You don't need to retype the FromFile statement—just type the Try, Catch, MsgBox, and End Try statements above and below it. If Visual Studio adds Catch, variable declaration, or End Try statements in the wrong place, simply delete the statements and retype them as shown in the book. (The Code Editor tries to be helpful, but its auto complete feature sometimes gets in the way.)

This program code demonstrates the most basic use of a Try…Catch code block. It places the problematic FromFile statement in a Try code block so that if the program code produces an error, the statements in the Catch code block are executed. The Catch code block simply displays a message box asking the user to insert the required disc in drive D so that the program can continue. This Try…Catch code block contains no Finally statement, so the error handler ends with the keywords End Try.

Again, if you are using a removable storage device or media associated with a different drive letter, you would make those changes in the statements that you just typed.

  1. Remove the CD from drive D, and click the Start Debugging button to run the program.

  2. Click the Check Drive button.

    Instead of stopping program execution, Visual Basic invokes the Catch statement, which displays the following message box:

    graphic

  3. Click OK, and then click the Check Drive button again.

    The program displays the message box again, asking you to insert the disc properly in drive D. Each time there's a problem loading the file, this message box appears.

  4. Insert the disc in drive D, wait a moment for the system to recognize the CD (close any windows that appear when you insert the disc), click OK, and then click the Check Drive button again.

    The bitmap graphic appears in the picture box, as expected. The error handler has completed its work effectively—rather than the program crashing inadvertently, it's told you how to correct your mistake, and you can now continue working with the application.

  5. Click Close on the form to stop the program.

It's time to learn some of the variations of the Try…Catch error handler.

Using the Finally Clause to Perform Cleanup Tasks

As the syntax description for Try…Catch noted earlier in the chapter, you can use the optional Finally clause with Try…Catch to execute a block of statements regardless of how the compiler executes the Try or Catch blocks. In other words, whether or not the Try statements produced a run-time error, there might be some code that you need to run each time an error handler is finished. For example, you might want to update variables or properties, display the results of a computation by using a message box or other mechanism, or perform “cleanup” operations by clearing variables or disabling unneeded objects on a form.

The following exercise demonstrates how the Finally clause works, by displaying a second message box whether or not the FromFile method produces a run-time error.

Use Finally to display a message box

  1. Display the Button1_Click event procedure, and then edit the Try…Catch code block so that it contains two additional lines of code above the End Try statement. The complete error handler should look like this:

    Try     PictureBox1.Image = _       System.Drawing.Bitmap.FromFile("d:\fileopen.bmp") Catch     MsgBox("Please insert the disc in drive D!") Finally     MsgBox("Error handler complete") End Try

    The Finally statement indicates to the compiler that a final block of code should be executed whether or not a run-time error is processed. To help you learn exactly how this feature works, I've inserted a MsgBox function to display a test message below the Finally statement. Although this simple use of the Finally statement is helpful for testing purposes, in a real program you'll probably want to use the Finally code block to update important variables or properties, display data, or perform other cleanup operations.

  2. Remove the CD from drive D, and then click the Start Debugging button to run the program.

  3. Click the Check Drive button.

    The error handler displays a dialog box asking you to insert the disc in drive D.

  4. Click OK.

    The program executes the Finally clause in the error handler, and the following message box appears:

    graphic

  5. Click OK, insert the disc in drive D, and then click the Check Drive button again.

    The file appears in the picture box as expected. In addition, the Finally clause is executed, and the “Error handler complete” message box appears again. As I noted earlier, Finally statements are executed at the end of a Try…Catch block whether or not there's an error.

  6. Click OK, and then click the Close button on the form to stop the program.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net