1020 1024




Visual Basic 6 Black Book:Error Handling And Debugging
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive To access the contents, click the chapter and section titles. Visual Basic 6 Black Book (Publisher: The Coriolis Group) Author(s): Steven Holzner ISBN: 1576102831 Publication Date: 08/01/98 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




Getting An Error’s Error Code
To determine what trappable error has occurred, you can use the Visual Basic Err object’s 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.
Let’s see an example. Here, we use the Err object’s 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



TIP:  These days, it’s considered bad programming practice to simply display an error number to the user—the user might not know, for example, that error 31001 means “out of memory.” To translate an error code into an error description, you can pass that code to the Visual Basic Error function, which returns the text error message for that error, and you can display that. You can also use the Err object’s Description property, as we’ll see in the next topic.

Getting An Error’s Description
To let the user know what kind of trappable error has occurred, you can use the Visual Basic Err object’s Description property. Let’s see an example. In this code, we’ll 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, we’ll 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 occurred—and indicate what error occurred with readable text.
Determining An Error’s Source Object
You can determine the object that caused the error using the Visual Basic Err object’s 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 Property
Errors 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 object’s LastDLLError property.
Creating An Intentional (User-Defined) Error
There 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 that’s incompatible with your program’s logic. You can create an error intentionally, called raising an error, with the Visual Basic Err object’s Raise method:


Err.Raise number, [source [, description [, helpfile [, helpcontext]]]]


Here are the arguments for the Raise method:

•  number—Long integer that identifies the nature of the error (see the paragraphs that follows for more details).
•  source—String expression naming the object or application that generated the error; use the form project.class. (If the source is not specified, the name of the current Visual Basic project is used.)
•  description—String expression describing the error.
•  helpfile—The path to the Help file in which help on this error can be found.
•  helpcontext—The context ID identifying a topic within helpfile that provides help for the error.

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.

Let’s see an example. Here, we’ll 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 we’re raising errors intentionally in Visual Basic.


Figure 29.1  Generating an error on purpose.

TIP:  You can also use the Visual Basic Error statement to raise an error like this: Error errnumber. However, the Error function is considered obsolete now, replaced by the Raise method of the Err object.





Previous
Table of Contents
Next






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.



Wyszukiwarka

Podobne podstrony:
1016 1020
1024 1
avt 1024 Słuchawkowy wzmacniaczn wys jakości
1016 1020
Brother Fax 1010, 1020, 1030, MFC 1970mc Parts Manual
1024 1029
1020 1
Lexmark 4078 00x Color JetPrinter 1020 Service Manual
Dz U 13 1020 ja
1024 (2)

więcej podobnych podstron