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:
·
The scripting language in which the error occurred.
·
The error number.
·
A short description of the error.
·
The name of the .asp file.
·
The line number where the error occurred.
·
The source code in which the error occurred.
·
A long description of the error and a possible fix for 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.
= <br>
There are values for .<br>
Value is
<b></b><br>
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:
·
Misspelling of terms.
·
Incorrect capitalization (JScript is case-sensitive).
·
Unmatched pairs of brackets, parentheses, and single and double
quotation marks.
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.
Database Error Trapping Routine
<%
If err.number > 0 Then
Response.write("VBScript Errors Occured:" &
"<P>")
Response.write("Error Number=" & err.number &
"<P>")
Response.write("Error Descr.=" & err.description
& "<P>")
Response.write("Help Context=" & err.helpcontext
& "<P>")
Response.write("Help Path=" & err.helppath &
"<P>")
Response.write("Native Error=" & err.nativeerror
& "<P>")
Response.write("Source=" & err.source &
"<P>")
Response.write("SQLState=" & err.sqlstate &
"<P>")
Else
Response.write("No VBScript Errors Occured" &
"<P>")
End If
If conn.errors.count> 0 then
Response.write("Database Errors Occured" &
"<P>")
For counter= 0 to conn.errors.count
Response.write("Error #" &
conn.errors(counter).number & "<P>")
Response.write("Error desc. -> "
&
conn.errors(counter).description & "<P>")
Next
Else
Response.write "No Database Errors Occured!" &
"<P>"
End If
%>