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
Nested Error Handling If a trappable error occurs in a procedure, you can handle that error in an error handler. But what if you call another procedure, and an error occurs before control returns from that procedure? If the called procedure has an error handler, the code in that error handler will be executed. However, if the called procedure does not have an error handler, control will return to the error handler in the calling procedure. In this way, control moves back up the calling chain to the closest error handler.
Lets see an example. Here, we set up an error handler in a subroutine and then call another subroutine, LoadText, to load text from a file the user specifies into a text box, Text1:
Private Sub Command1_Click()
On Error GoTo FileError
LoadText
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
Heres the LoadText subroutine, which uses an Open Common Dialog to get the name of the file to open, opens the file, and loads the files text into Text1:
Public Sub LoadText() With CommonDialog1
.ShowOpen Open .FileName For Input As #1 Text1.Text = Input$(LOF(1), #1) Close #1
End With End Sub
If an error occurs in LoadText, which has no error handler itself, control returns back to the calling subroutine, and the code in the error handler there is executed. In this way, you dont have to worry about calling procedures without error handlers when youre trying to trap errors. Creating An Error Object Directly In Visual Basic You can create an error object directly and use it as a return value from your procedures. To create that object, you use the Visual Basic CVErr function, passing it the error code for the error you want to create. Lets see an example. Here, well set up a function, MakePositiveNumber, which takes a string representing an integer (for example, 25) and returns the integer represented by that string (for example, 25). If you pass MakePositiveNumber a string representing a negative integer (for example, 25), the function will return an error object (actually a Variant of subtype Error). We start by declaring MakePositiveNumber, indicating that it will accept a string and return a variant (that variant can be either a number or an error object):
Public Function MakePositiveNumber(strData As String) As Variant ...
Next, we use the Visual Basic Val function to create an integer from the string passed to us (to keep this example short, were assuming the string passed to MakePositiveNumber does indeed represent a valid positive or negative integer):
Public Function MakePositiveNumber(strData As String) As Variant Dim intValue As Integer
intValue = Val(strData) ...
If the integer is less than 0, we return an error with the error code 5 (which in Visual Basic means Invalid procedure call) using CVErr this way:
Public Function MakePositiveNumber(strData As String) As Variant Dim intValue As Integer
intValue = Val(strData) If intValue < 0 Then MakePositiveNumber = CVErr(5) ...
Otherwise, we return the positive integer this way:
Public Function MakePositiveNumber(strData As String) As Variant Dim intValue As Integer
intValue = Val(strData) If intValue < 0 Then MakePositiveNumber = CVErr(5) Else MakePositiveNumber = intValue End If End Function
Now well put MakePositiveNumber to work. When the user clicks a command button, Command1, we can take the string in a text box, Text1, use MakePositiveNumber to convert it to a number, and display the result in another text box, Text2:
Private Sub Command1_Click() Dim varNumber As Variant
varNumber = MakePositiveNumber(Text1.Text)
Text2.Text = Str(varNumber) End Sub
We can check for error return values with the Visual Basic IsError function, which returns True if you pass it an error object. If MakePositiveNumber did return an error, we convert that error into a number with the Visual Basic CInt function, and if that error is 5, indicating that the passed string represented a negative number, we display a message box informing the user that the number to convert must be positive:
Private Sub Command1_Click() Dim varNumber As Variant
varNumber = MakePositiveNumber(Text1.Text)
If IsError(varNumber) Then If CInt(varNumber) = 5 Then MsgBox "Number must be positive" End If Else Text2.Text = Str(varNumber) End If End Sub
And thats itweve created and used our own error objects. When you run the program and try to convert a negative value, as shown in Figure 29.2, the program displays an error. The code for this example is located in the errobject folder on this books accompanying CD-ROM.
Figure 29.2 Using a user-defined error object in Visual Basic. Trappable Cancel Errors In Common Dialogs The usual way to work with the Cancel button in Common Dialogs is to use trappable errors. You set the Common Dialog controls CancelError property to True, and the Common Dialog control will generate an error when the user clicks the Cancel button. Lets see an example. Here, well use a Common Dialog to open a text file the user has selected and display it in a text box, Text1. If the user has clicked the Cancel button, the trappable error cdlCancel is generated, and we display a message box asking the user to select a file and try the operation again by showing the Open dialog box again:
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 Default MsgBox "File Error" End Select