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.
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.
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 ' Code here attempts to do something. Catch ' If an error occurs, code here will run. Finally ' Code in this block will always run. End Try
Try It!
To use the Try...Catch block
- On the File menu, choose New Project.
- In the New Project dialog box, in the Templates pane, click Windows Application.
- In the Name box, type TryCatch and then click OK.
A new Windows Forms project opens. - From the Toolbox, drag one TextBox control and one Button control onto the form.
- Double-click the Button to open the Code Editor.
- In the Button1_Click event handler, type the following code:
Try Dim aNumber As Double = CDbl(Textbox1.Text) MsgBox("You entered the number " & aNumber) Catch MsgBox("Please enter a number.") Finally MsgBox("Why not try it again?") End Try
- Press F5 to run the program.
- 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.
- 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.
No comments:
Post a Comment