|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Getting An Errors Error CodeTo determine what trappable error has occurred, you can use the Visual Basic Err objects Number property, which holds the error code; see Table 29.1 at the beginning of this chapter for a list of trappable errors and their numeric codes. Lets see an example. Here, we use the Err objects Number property to set up a Select Case statement, handling trappable errors in different ways. If the error occurred because the users clicked the Cancel button in the Open dialog box, we indicate to the users that they should select a file and retry the operation. If the error occurred because the users entered the name of a file that was subsequently not found (trappable error 53), we indicate that to them. Otherwise, we just display the generic message File error in a message box:
Private Sub Command1_Click()
On Error GoTo FileError
With CommonDialog1
.ShowOpen
Open .FileName For Input As #1
Text1.Text = Input$(LOF(1), #1)
Close #1
End With
Exit Sub
FileError:
Select Case Err.Number
Case cdlCancel
MsgBox "Please select a file."
Resume
Case 53
MsgBox "File not found"
Case Default
MsgBox "File Error"
End Select
End Sub
Getting An Errors DescriptionTo let the user know what kind of trappable error has occurred, you can use the Visual Basic Err objects Description property. Lets see an example. In this code, well trap errors and handle two of them expressly: the case where the user has clicked the Cancel button in the Open Common Dialog, and the case where a File Not Found error occurs. Otherwise, well just display the Visual Basic error message in the Default case of a Select Case statement:
Private Sub Command1_Click()
On Error GoTo FileError
With CommonDialog1
.ShowOpen
Open .FileName For Input As #1
Text1.Text = Input$(LOF(1), #1)
Close #1
End With
Exit Sub
FileError:
Select Case Err.Number
Case cdlCancel
MsgBox "Please select a file."
Resume
Case 53
MsgBox "File not found"
Case Default
MsgBox Err.Description
End Select
End Sub
Using Err.Description, you can inform the user that an error occurredand indicate what error occurred with readable text. Determining An Errors Source ObjectYou can determine the object that caused the error using the Visual Basic Err objects Source property. This property holds the name of the object or application that caused the error. For example, if you connect to Microsoft Excel and it generates an error, Excel sets Err.Number to its error code for that error, and it sets Err.Source to Excel.Application. Handling Errors In DLLs: The LastDLLError PropertyErrors during calls to Windows dynamic link libraries (DLLs) do not create trappable errors and so cannot be trapped with Visual Basic error trapping. In practice, this means that when you call a DLL procedure, you should check each return value for success or failure. In the event of a failure, check the value in the Err objects LastDLLError property. Creating An Intentional (User-Defined) ErrorThere are cases in programs where you might want to create an error; although no Visual Basic trappable error has occurred, some situation may have occurred thats incompatible with your programs logic. You can create an error intentionally, called raising an error, with the Visual Basic Err objects Raise method: Err.Raise number, [source [, description [, helpfile [, helpcontext]]]] Here are the arguments for the Raise method:
When setting the error number for the error, bear in mind that Visual Basic errors are in the range 0 to 65535. The range 0 to 512 is reserved for system errors, but the range 513 to 65535 is available for user-defined errors. Lets see an example. Here, well generate an error, error number 2000, when the user clicks a command button, Command1, and then indicate the error has occurred with a message box. First, we raise error 2000:
Private Sub Command1_Click()
On Error GoTo CaptionError
Err.Raise 2000
...
Then we display the error in a message box in an error handler:
Private Sub Command1_Click()
On Error GoTo CaptionError
Err.Raise 2000
Exit Sub
CaptionError:
MsgBox "Error number " & Err.Number
End Sub
The result appears in Figure 29.1. Now were raising errors intentionally in Visual Basic.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |