How do i debug vbscript




















In the absence of a debugger, many programmers and content providers inserted calls to the Window. Alert method for client-side scripting , to the Response. Write method for server-side scripting , or to the MsgBox function for WSH scripts and Outlook forms to serve as breakpoints in various places in a script. The dialog would then display the values of particular variables or expressions selected by the programmer.

In contrast, using the Command window to display the value of any non-object variable is easy. Simply type a question mark? The Script Debugger will then evaluate the variable and display its value in the Immediate window.

The debugger cannot evaluate the result of user-defined functions; it can evaluate only intrinsic functions functions that are a built-in part of the scripting language. Basically, the user should be able to enter a number and, if it is actually between zero and two, be shown the element of the array at that ordinal position. Somewhere in this code is a sneaky little bug causing problems.

The script always tells the user that the number entered into the text box is too large, which indicates that it is greater than the upper boundary of the array. To debug the script in Example , you can place a breakpoint on the first line of the sGetValue function, since this is probably where the problem lies.

Then run the script and enter the number 2 into the text box txtText1. As you can see, the call to the sGetValue function has a single argument, iTest , which is passed to the function as the iVal parameter. So our first step is to determine the value of iVal at runtime by entering the following into the Command window:.

Next, find out what the script thinks the upper boundary of the array is by entering the following in the immediate window and pressingEnter:. So iVal is not greater than UBound sTest. Next, go back to the script window and press F8 to follow the flow of program control. That indicates that the scripting engine has evaluated the expression incorrectly and has decided that iVal is greater then UBound sTest. So go back to the Command window, and this time try to evaluate the complete expression:.

Given this apparent incongruity, it seems likely that our problem may be centered in the data types used in the comparison. So try the following:. The result is:. And iTest in turn represents the value retrieved from the textbox, which of course must be string data, as typing the following into the Command window establishes:. You can see from this debugging exercise that the Command window is a powerful tool allowing you to perform function calls, evaluate complete expressions, and try out different ways of writing your code.

Another use for the Command window is to assign a new value to a variable. You can, however, correct the error and continue executing the script. Just place a breakpoint on the offending line and click on the button when the browser displays it so that the script executes. When program execution halts, you can check the value of myNum :. If you have experience with Visual Basic, the debugging concepts covered in this section will be familiar to you.

Because the scripting window is read-only, you cannot edit the code during execution, as you can most of the time with VB. This is particularly useful to backtrack or to re-execute a section of code.

Unfortunately, this feature is not available in the Script Debugger. Error handling does not involve finding errors in your scripts. Instead, use error-handling techniques to allow your program to continue executing even though a potentially fatal error has occurred.

Ordinarily, all runtime errors that are generated by the VBScript engine are fatal, since execution of the current script is halted when the error occurs. Error handling allows you to inform the user of the problem and either halt execution of the program or, if it is prudent, continue executing the program. There are two main elements to error handling in VBScript. The first is the On Error statement, which informs the VBScript engine of your intention to handle errors yourself, rather than to allow the VBScript engine to display a typically uninformative error message and halt the program.

This is done by inserting a statement like the following at the start of a procedure:. This tells the VBScript engine that, should an error occur, you want it to continue executing the program starting with the line of code that directly follows the line in which the error occurred. For example, in the simple WSH script:.

A particular On Error statement is valid until another On Error statement in the line of execution is encountered, or an On Error Goto 0 statement which turns off error handling is executed. Therefore, if an error occurs in Function B, it is the On Error statement in Function A that handles the error; in other words, when an error is encountered in Function B, program flow will immediately jump to the line of code that followed the call to Function B in Function A.

When Function A completes execution, the On Error statement it contains also goes out of scope. This means that, if the routine that called Function A did not include an On Error statement, no error handling is in place. VBScript includes an error object, named Err, which, when used in conjunction with On Error Resume Next , adds much more functionality to error handling, allowing you to build robust programs and relatively sophisticated error-handling routines.

ASP 3. Errors in ASP scripts are handled automatically by the web server in one of three ways: by sending a default message to the client, by sending the client the contents of a particular file, or by redirecting the client to an error-handling web page, depending on how the IIS has been configured. Within the error-handling page, the ASPError object can be examined to determine the cause of the error. In ASP 3. The Err object is part of the VBScript language and contains information about the last error to occur.

By checking the properties of the Err object after a particular piece of code has executed, you can determine whether an error has occurred and, if so, which one. You can then decide what to do about the error — you can, for instance, continue execution regardless of the error, or you can halt execution of the program. The main point is that error handling using On Error and the Err object puts you in control of errors, rather than allowing an error to take control of the program and bring it to a grinding halt.

Like all object properties, the properties of the Err object can be accessed by using the name of the object, Err, the dot or period delimiter, and the property name. The Err object supports the following properties:. The Number property is a Long value that contains an error code value between -2,,, and 2,,, The possibility of a negative error code value seems incongruous but results from the fact that error codes are unsigned long integers, a data type not supported by VBScript.

VBScript itself provides error code values that range from 0 to 65, COM components, however, often provide values outside of this range. If the value of Err.

Number is 0, no error has occurred. A line of code like the following, then, can be used to determine if an error has occurred:. Although the properties of the Err object provide information on the last error to occur in a script, they do not do so permanently.

In addition, you can explicitly reset Err. The WSH script in Example illustrates the importance of resetting the Err object after an error occurs. The division by zero on the fifth line of the script in Example generates an error. Therefore, the conditional statement on line 6 evaluates to True and an error dialog is displayed. Program flow then continues at line Line 12 is a perfectly valid assignment statement that always executes without error, but the Err.

Number property still contains the error number from the previous error in line 5. As a result, the conditional statement on line 13 evaluates to True , and a second error dialog is displayed. The Description property contains a string that describes the last error that occurred.

You can use the Description property to build your own message box alerting the user to an error, as the WSH script in Example shows. The Source property contains a string that indicates the class name of the object or application that generated the error. You can use the Source property to provide users with additional information about an error—in particular, about where an error occurred. Obviously, this makes the Source property less than useful in many cases.

However, you can assign a value to the Source property in your own error-handling routines to indicate the name of the function or procedure in which an error occurred.

In addition, the primary use of the Source property is to signal an error that is generated by some other object, like an OLE automation server such as Microsoft Excel or Microsoft Word.

The two methods of the Err object allow you to raise or clear an error, while simultaneously changing the values of one or more Err object properties.

The two methods are:. The Err. Raise method allows you to generate a runtime error. Its syntax is: [ 1 ]. At first glance, generating an error within your script may seem like a very odd thing to want to do! However, there are times, particularly when you are creating large, complex scripts, that you need to test the effect a particular error will have on your script.

The easiest way to do this is to generate the error by using the Err. Raise method and providing the error code to the ErrorNumber parameter, then sit back and note how your error-handling routine copes with the error, what the consequences of the error are, and what side effects the error has, if any. The client-side script in Example , for instance, allows the user to enter a number into a text box, which is passed as the error code value to the Err.

Raise method. If the value of the error code is non-zero, an Alert box opens that displays the error code and its corresponding description. Figure , for instance, shows the Alert box that is displayed when the user enters a value of 13 into the text box. Table lists a few of the most common runtime errors. The Clear method clears the information that the Err object is storing about the previous error; it takes no parameters.

Leo Chapiro Jan Abhishek Kumar Goswami Sep Leo Chapiro Sep SmokeHead Sep Emmet M Jan Nelek Jan If something has a solution Why do we have to worry about?. If it has no solution For what reason do we have to worry about?

Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer. Akhil Mittal Jan Thanks Do not forget to comment and rate the article if it helped you by any means.

Go to top. Layout: fixed fluid. First Prev Next. My vote of 5 Gustav Brock 3-Dec Great tip! Re: My vote of 5 Leo Chapiro 4-Dec Gustav, thank you so much for your feedback!

Very helpful, thank you! I work with VBS, this tip is very helpful for me, thank you a lot! Re: Very helpful, thank you! You are welcome, I am glad to help! Possible typo Member Jan Member Re: Possible typo Leo Chapiro Jan Thank you, Member , I've edited! My vote of 1 Abhishek Kumar Goswami Sep There are certain flags in the registry and, depending on the debugger used, certain required procedures to enable debugging. The script debugger should be Msscrdbg.

To do this, follow these steps:. Click Debugger, and then ensure that the Just-In-Time options are selected for both the General and Script categories.

Additionally, if you are trying to debug a. Click the mse7. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?

Collectives on Stack Overflow. Learn more. How do I debug a stand-alone VBScript script? Ask Question. Asked 11 years, 9 months ago. Active 2 years, 7 months ago. Viewed k times. Improve this question. STF 1, 3 3 gold badges 17 17 silver badges 32 32 bronze badges. Dheer Dheer 3, 6 6 gold badges 33 33 silver badges 45 45 bronze badges. Add a comment. Active Oldest Votes. Run cscript. Improve this answer.



0コメント

  • 1000 / 1000