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
Using On Error GoTo Label
The Visual Basic On Error GoTo statement is the foundation of handling trappable errors. When you execute an On Error GoTo Label statement in your code, execution is transferred to the code starting at Label if a trappable error has occurred. The code following that label is your error handler.
Lets see an example to make this clear. In the previous topic, we executed a statement indicating that our error handler code starts at the label FileError this way:
Private Sub Command1_Click()
On Error GoTo FileError
...
Now if an error occurs, well transfer program execution to the code that follows the label FileError. That means that for all this code, error trapping is enabled:
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
...
The actual error-handling code itself follows the label FileError like this:
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: Note that if you want to turn off error trapping at some point in your code, you can execute the statement On Error GoTo 0 (see Using On Error GoTo 0 coming up later in this chapter). You can also redirect error trapping to a new error handler by executing a new On Error GoTo Label statement.
Using On Error GoTo line#
Besides using a label to start an error handler (see the previous topic), you can refer to an error handler by line number in Visual Basic, using the On Error GoTo line# statement. Numbering code lines is part of Visual Basic history all the way back to the original days of the Basic language, and, in fact, many programmers dont know that you can number the lines of code in Visual Basic. For example, heres how we set up an error handler that starts at line 16 in our code (you can enter the line numbers directly in the code as shown in the following code), using the On Error GoTo line# statement:
Private Sub Command1_Click()
1
2 On Error GoTo 16
3
4 With CommonDialog1
5
6 .ShowOpen
7 Open .FileName For Input As #1
8 Text1.Text = Input$(LOF(1), #1)
9
10 Close #1
11
12 End With
13
14 Exit Sub
15
16
17 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
Now when theres a trappable error, program execution jumps to line 16 to execute our error handler. Note that numbering lines has long been obsolete in Visual Basic; for most purposes, its better to stick with On Error GoTo Label.
Using On Error Resume Next
The On Error Resume Next statement provides an easy way to disregard errors, if you want to do so. Once you execute this statement, execution continues with the next line of code if the current line generates an error, and the error is disregarded.
Lets see an example. Here we set the Text, Caption, Min, and Max properties of the currently active control on a form when the user clicks that form. Because no one control has all those properties, this code would generate an error in a message box to the user:
Private Sub Form_Click()
ActiveControl.Text = "Active control"
ActiveControl.Caption = "Active Control"
ActiveControl.Min = 0
ActiveControl.Max = 100
End Sub
However, we can place an On Error Resume Next statement in the code to suppress all the trappable errors:
Private Sub Form_Click()
On Error Resume Next
ActiveControl.Text = "Active control"
ActiveControl.Caption = "Active Control"
ActiveControl.Min = 0
ActiveControl.Max = 100
End Sub
The result here is that whichever of these four properties the active control does have is set, without any errors.
WARNING! Note, however, that code like this is not good programming form, of course; its better to check what the type of the active control is before setting its properties instead of simply disregarding errors.
Using On Error GoTo 0
To turn off error trapping, you can use the On Error GoTo 0 statement. For example, here we turn on error trapping to catch the case where the user clicks the Cancel button in a Common Dialog controls Font dialog box, but we then turn error trapping off if the user did not press Cancel (to make the Cancel button generate a trappable error, set the Common Dialog controls CancelError property to True; this is the standard way of catching the Cancel button when working with Common Dialogs):
Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.Flags = cdlCFBoth Or cdlCFEffects
CommonDialog1.ShowFont
On Error GoTo 0
Text1.FontName = CommonDialog1.FontName
Text1.FontBold = CommonDialog1.FontBold
Text1.FontItalic = CommonDialog1.FontItalic
Text1.FontUnderline = CommonDialog1.FontUnderline
Text1.FontSize = CommonDialog1.FontSize
Text1.FontName = CommonDialog1.FontName
Cancel:
End Sub
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 10201012 1Braun golarka męska Entry, 100, 105, 1007, 1008, 1012, 1013 instrukcja obsługiTYN0512 1012 TXN0512 10121016 1020KDC 1016 KDC 115S1016 11016 (2)więcej podobnych podstron