ASP Lecture Series (Seventeen) Debugging ASP Scripts
Author:Eve Cole
Update Time:2009-05-30 19:58:42
No matter how sophisticated your planning and experience, script errors (bugs) may prevent your ASP server-side scripts from running correctly in the first place. That is to say, debugging, that is, finding and correcting script errors, is very important to developing a successful and robust ASP program.
Microsoft Script Debugging Tool
Microsoft® Script Debugger is a powerful debugging tool that helps you quickly find errors and interactively test server-side scripts. Script Debugger works with Windows Internet Explorer version 3.0 or later. With it you can:
Run the server-side script line by line.
Open a command window to monitor the value of a variable, property, or array element while the server-side script is executing.
Set a pause flag on a specific line to suspend server-side scripts (using debugging tools or script commands).
Track the process while running a server-side script.
Note You can use debugging tools to view the script and find errors, but you cannot edit the script directly. To correct errors, you must edit the script with an editor, save it, and then run the script.
(This feature is not available on Windows 95 or later.)
Enabling debugging Before you start debugging server-side scripts, you must first configure your Web server to support ASP debugging.
After you enable web server debugging, you can use any of the following methods to debug your scripts:
Manually open the Microsoft Script Debugger and debug the ASP server-side script.
Use Internet Explorer to request the .asp file. If the file contains errors or artificially added stop execution statements, the Microsoft Script Debugger automatically starts, displays the script, and indicates the source of the error.
Script Errors When debugging server-side scripts, you may encounter various types of errors. Some of these errors prevent the script from executing correctly, causing the program to stop executing, or returning incorrect results.
Syntax Errors Syntax errors are frequently encountered errors that are caused by incorrect script syntax. For example, a misspelled command or incorrect parameters passed to a function will generate an error. Syntax errors may prevent your script from running.
Runtime Errors Runtime errors are caused by script instructions trying to perform impossible actions during script execution. For example, the following script contains a function that divides by zero (an illegal mathematical operation), which will generate a runtime error:
<script language = "VBScript" runat = server>
Result = Findanswer(15)
document.write ("The answer is " &Result)
Function Findanswer(x)
'This statement generates a run-time error.
Findanswer = x/0
End Function
</script>
Errors that cause run-time errors must be corrected so that the script does not interrupt while it is running.
Logic errors Logic errors can often be latent and difficult to detect. If there are logic errors caused by typing errors or incorrect program logic flow, the script may run successfully but produce incorrect results. For example, a server-side script that plans to sort a list of values will return incorrect sorting results if the "<" symbol is used to compare values instead of the ">" symbol.
Error Debugging Techniques A number of different debugging techniques can be used to find the source of errors and test applications.
Just-In-Time (JIT) debugging When a run-time error interrupts the execution of an ASP script, the Microsoft Script Debugger automatically starts, displays the .asp file, points the statement pointer to the line that caused the error, and generates an error message. This type of debugging is called just-in-time (JIT) debugging, where the computer pauses the execution of the program. You must use your editing program to correct these errors, save your corrections, and then run the script again.
Breakpoint debugging When an error occurs and it is difficult to find the source of the error, you can use the debugging method of preset breakpoints. Breakpoints pause the script at a specific line. You can set one or more different breakpoints at the problematic points in the script, and then use the debugger to correct the values of variables or properties set in the script. After you correct these errors, you can clear the breakpoints so that your script runs uninterrupted.
Please use the Microsoft Script Debugger to open the script and set breakpoints. Then request this script with your web browser. When a line containing a breakpoint in the script is executed, the computer starts the script debugger, and the statement pointer points to the line where the breakpoint is set.
VBScript Stop Statement Debugger You can also add breakpoints to server-side scripts written in VBScript by inserting a Stop statement before the problematic portion of the server-side script. For example, the following ASP script contains a Stop statement to pause execution before displaying results:
<%
dayvalue = 3
TheDay = WeekDayName(dayvalue)
Stop 'set breakpoint here.
Response.Write("Today is " + TheDay)
%>
When you request the previous script used by the Web browser, the debugger starts and automatically displays the .asp file, and the statement pointer indicates the location of the stop statement. Always remember to remove the Stop statement from the delivered .asp file.
Debugging with JScript Debugger Statements To add breakpoints to server-side scripts written in Microsoft® JScript, insert a debugger statement before the line in question. For example, the following script contains a debugger statement that interrupts execution and automatically starts the Microsoft Script Debugger each time the script loops to a new value.
<%@ Language="JScript" %>
<%
for (var count = 1; count <= 10; count++) {
var eventest = count%2;
debugger //Sets breakpoint
if (eventest == 0) {
response.write("Even value is " + count + "<br>")
}
}
%>
Remember to remove the debugger statements from the delivered .asp file.
Note: Do not confuse debugger statements with JScript break statements. During program execution, the break statement only exits the current loop and does not activate the Microsoft Script Debugger or pause program execution.
Script Debugging Techniques In addition to script debuggers, a good set of debugging techniques can also reduce a lot of time spent analyzing the source of script errors. While most errors are caused by obvious causes, misspelled commands or missing variables, some type of logic and execution errors can also be caused by less obvious causes.