Debugging Active Server Pages Scripts

When you write out a script, both technical and syntactical accuracy are required. Inaccuracy may prevent a script from running properly, and may generate error messages. The process of resolving these messages is called debugging. Thoroughly testing your scripts, and debugging them if necessary, insures that all users who visit your Web site will get the intended experience rather than an error message.

Error messages are sent back as HTML with all or some the following information, depending upon the nature of the error:

Note   Using a text editor that displays line numbers will help you locate a line with an error in your .asp file.

Use this information to modify .asp files so that errors can be resolved. Errors of a severe nature will be sent to the Windows NT log and the Internet Information Server (IIS) log, as well as to the client browser. All other errors will be sent to the IIS log and client browser.

Error Handling with VBScript

The On Error Resume Next Statement

If an error is encountered in your .asp file, the processing of your script stops and an error message is returned to the browser. If you want to continue processing your page even if an error is encountered, include the following line at the beginning of your .asp file:

<% On Error Resume Next %> 

Note   The On Error Resume Next statement is a VBScript statement; it affects only scripts written in VBScript. Using this statement within an .asp file containing JScript will have no effect on JScript error debugging because JScript has no functional equivalent of resuming after an error. If you call a JScript function from VBScript and the JScript function causes an error, an error message is returned to the browser and processing of the .asp file stops at that point.

Using the On Error Resume Next statement does not actually clear an error; to manually clear the error, you can use the Err.Clear method:

<HTML>
<HEAD>
<TITLE>Error Handling with On Error Resume Next and Err.Clear</TITLE> 
</HEAD>
<BODY>
<% 
Call DoSafeDivide(1, 3) 
Call DoSafeDivide(1, 0) 
%>

<SCRIPT LANGUAGE="VBScript" RUNAT=Server>

Sub DoSafeDivide(x, y)
 
       On Error Resume Next

       z = x / y
       If Err.Number > 0 Then
 	 Response.Write("Division failed:  " & x & " / " & y & "<BR>")
 	 Response.Write("Error source: " & Err.Source & "<BR>")
 	 Response.Write("Error number: " & Err.Number & "<BR>")
 	 Response.Write("Error description: " & Err.Description & "<BR>")
               Err.Clear
       Else
 	 Response.Write("Division succeeded: " & x & " / " & y & " = " & z & "<BR>")
       End If
End Sub

</SCRIPT>
</BODY>
</HTML>

The For...Each Statement

You can use the For...Each statement to iterate over a collection, thus simplifying your debugging process by returning all of the variables of a collection in a script. For example, the following script sample uses the For...Each statement with the QueryString collection of the Request object to return all values for all keys in the QueryString collection.

<% For Each Key In Request.QueryString %>
  <%= Key %> = <%= Request.QueryString(Key) %><br>
  <% ValueCount = Request.QueryString(Key).Count
    If ValueCount > 1 Then %>
      There are <%= ValueCount %> values for <%= Key %>.<br>
    <% For i = 1 To ValueCount %>
        Value <%= i %> is <b><%= Request.QueryString(Key)(i) %></b><br>
    <% Next
    End If   
  Next
%>

VBScript errors can generate a pointer which indicates the exact location of the error in the script code:

Microsoft VBScript compilation error '800a03f3' 

Expected '=' 

/ASPSamp/Samples/outstrem.asp, line 11 

Set OutStream Nothing
-------------^ 

Error Handling with JScript

The following scripting mistakes commonly result in errors for JScript:

Always check for these types of errors when debugging JScript.

Debugging Forms

If you are receiving variables in the QueryString collection of the Request object that should be in the Form collection, make sure that the HTML <FORM> tag is setting METHOD=POST. The GET method passes form variables into the QueryString collection. The POST method passes form variables into the Form collection.


© 1996 Microsoft Corporation. All rights reserved.