One Step Further: The Exit Try Statement


One Step Further: The Exit Try Statement

You've learned a lot about error handlers in this chapter; now you're ready to put them to work in your own programs. But before you move on to the next chapter, here's one more syntax option for Try…Catch code blocks that you might find useful: the Exit Try statement. Exit Try is a quick and slightly abrupt technique for exiting a Try…Catch code block prematurely. If you've written Visual Basic programs before, you might notice its similarity to the Exit For and Exit Sub statements, which you can use to leave a structured routine early. Using the Exit Try syntax, you can jump completely out of the current Try or Catch code block. If there's a Finally code block, this code will be executed, but Exit Try lets you jump over any remaining Try or Catch statements you don't want to execute.

The following sample routine shows how the Exit Try statement works. It first checks to see whether the Enabled property of the PictureBox1 object is set to False, a flag that might indicate that the picture box isn't ready to receive input. If the picture box isn't yet enabled, the Exit Try statement skips to the end of the Catch code block, and the file load operation isn't attempted.

Try     If PictureBox1.Enabled = False Then Exit Try     PictureBox1.Image = _       System.Drawing.Bitmap.FromFile("d:\fileopen.bmp") Catch     Retries += 1     If Retries <= 2 Then         MsgBox("Please insert the disc in drive D!")     Else         MsgBox("File Load feature disabled")         Button1.Enabled = False     End If End Try

The example builds on the last error handler you experimented with in this chapter (the Disc Drive Handler project). If you'd like to test the Exit Try statement in the context of that program, load the Disc Drive Handler project again and enter the If statement that contains the Exit Try in the Code Editor. You'll also need to use the Properties window to disable the picture box object on the form (in other words, set its Enabled property to False).

Congratulations! You've learned a number of important fundamental programming techniques in Visual Basic, including how to write error handlers. Now you're ready to increase your programming efficiency by learning to write Visual Basic modules and procedures.



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