iis| Script
Debugging ASP Scripts
Whether you have experience or not, you may encounter program errors, or "bugs," which will affect server-side scripts working properly. Therefore, debugging (the process of discovering and correcting script errors) is extremely important for developing successful and powerful ASP applications, especially when applications become more and more complex.
Microsoft Scripting Debugger Tools
The Microsoft Scripting Debugger tool is a powerful debugging tool that can help you quickly locate errors and test server-side scripts interactively. You can use a script debugger or work with Windows Internet Explorer 3.0 or later, and you can:
- Run one row of server-side scripts at a time.
- During server-side script execution, open a command window to monitor the values of variables, properties, or array elements.
- Set a breakpoint on the specified line of the script (using a debugger or script command) to suspend the execution of the server-side script.
- Tracks procedures when running server-side scripts.
AttentionYou can use the debugger to view scripts and locate errors, but you cannot edit the script directly. To correct an error, you must use an editing program to edit the script, save the changes, and then run the script again.
Enable debugging
Before you start debugging server-side scripts, you must first configure your WEB server to support ASP debugging. For instructions and information, see Enabling ASP Debugging.
After you enable WEB server debugging, you can debug the script using either of the following methods:
- Manually open the Script Debugger to debug an ASP server-side script.
- Use Internet Explorer to request. asp files. If the file contains errors or deliberately used to break execution statements, the script debugger starts automatically, displays the script, and marks the source of the error.
Script Error
There are several types of errors that you may encounter during debugging server-side scripts. Some errors can cause script execution errors, interrupt execution of programs, or return incorrect results.
Syntax error
A "syntax" error is a common error that is caused by the wrong scripting syntax. For example, a command spelling error or a parameter value error passed to a function can cause an error. Syntax errors can prevent scripts from running.
Run-time error
A run-time error occurs after the script starts executing, and it is caused by a script instruction that attempts to perform an impossible operation. For example, the following script contains a function that takes 0 as the divisor of a variable (illegal mathematical operation), resulting in a run-time error:
<script language=vbscript Runat=server>result = findanswer document.write ("result is" &result) Function Findanswer (x) ' This statement produces a run-time error. Findanswer = X/0end function</script>
The Run-time error must be corrected before the script executes without interruption.
Logical error
A logical error is the most difficult to find. A logical error is caused by a typing error or a procedural logic flaw, and the script runs fine, but the resulting result is incorrect. For example, if the server-side script will sort the values in the list, but the use of the > symbol (greater-than) in the script where the < symbol (less than) should have been used incorrectly, it will result in incorrect sorting results.
Error Debugging Technology
You can use several different debugging techniques to locate an error source and test your application.
Just-in-time (JIT) debugging
When a run-time error interrupts server-side script execution, the Microsoft script Debugger starts automatically, displays an. asp file, points the statement pointer to the row that caused the error, and generates an error message. With this type of debugging, also called "Just-in-time debugging," the computer pauses further execution of the program. You must use the edit program to correct the error and save the changes before you can continue running the script.
Breakpoint Debugging
It can sometimes be useful to preset breakpoints when an error occurs but you cannot easily find the source of the error. The breakpoint pauses execution of the script at the specified script line. You can set one or more different breakpoints before a suspicious line, and then use the debugger to check the values of the variables or properties set in the script. After correcting the error, you can clear the breakpoint so that the script can run uninterrupted.
To set breakpoints, open the script using a script debugger, select the line in the script that you want to break, and choose Toggle Breakpoint from the Debug menu. Then use the Web browser to request the script again. After executing the script line that sets the breakpoint, the computer starts the Script debugger, displays the script, and the statement pointer points to the row where the breakpoint is set.
The next statement is interrupted
In some cases, if the next statement is not running in an. asp file that you are working in, you might want to enable the "Next statement break" of the script Debugger. For example, if you set the next statement break for an. asp file residing in an application that is named Sales, the debugger starts when you run a script in any file of the sales application (or the application that has debugging enabled). In view of this, when you set the Next statement break, you need to be aware that the debugger will start regardless of which script statement is running in the next line.
Stop statement Debugging in VBScript
You can also add breakpoints to a server-side script written in VBScript by inserting a "Stop" statement somewhere before the suspect part of the server-side script. For example, the following server-side script contains a "Stop" statement that pauses execution of the script before the script calls the Custom function:
<%intday = Day (now ()) Lngaccount = Request.Form ("AccountNumber") Dtmexpires = Request.Form ("ExpirationDate") Strcustomerid = "RETAIL" & Intday & Lngaccount & Dtmexpires ' Set breakpoint here Stop ' invokes the registration component. RegisterUser (Strcustomerid)%>
When the script is requested, the debugger starts and automatically displays the. asp file and uses the statement pointer to indicate the position of the "Stop" statement. At the breakpoint, you can choose to check the value assigned to the variable before passing the variable to the component.
Main pointsYou must remove the "Stop" statement from the generated. asp file.
Debugger statement Debugging for JScript
To add a breakpoint to a server-side script written in VBScript, insert the "debugger" statement before the suspect line of the script. For example, the following script contains the "debugger" statement, which is used to break execution and automatically start the script debugger each time the script loops through a new value.
<%@ Language=jscript%><%for (var count = 1; count <= count++) {var eventest = count%2//Set breakpoints so that users can step into the script. Debuggerif (Eventest = = 0) Response.Write ("Even value is" + Count + <br>)}%>
The "debugger" statement must be removed from the generated. asp file.
AttentionDo not confuse the "debugger" statement with the "break" statement of JScript. The break statement exits a loop that is currently running during execution, does not activate the Microsoft Script Debugger, and does not suspend execution.
Techniques for debugging scripts
In addition to the Scripting debugger, a set of excellent debugging techniques can greatly reduce the time spent studying the source of a script error. Although most errors have obvious sources, such as command spelling errors or missing variables, there are certain types of logic and execution errors that are difficult to discover.
For more information about the Microsoft Scripting debugger, see the Microsoft Scripting Technology site at http://msdn.microsoft.com/scripting/.
©1997-1999 Microsoft Corporation. All rights reserved.