Easy Error Management - Third Trick: The Quick Debugger
(Page 4 of 4 )
If you’re at all like me, you will quickly be annoyed by typing and re-typing the same lines of code to read your offending variables, their type, size, and so forth. So, it’s always handy to have a subprocedure do all the work for you. Here’s a simple one:
'=========================================================
Sub debug( var )
'=========================================================
Response.Write("<span style=""font-size: 9pt;font-family: arial; font-weight: normal"">")
Response.Write("<strong>Type = " & typeName( var ) & "
")
If Not typeName( var ) = "Variant()" Then
Response.Write( "Size = " & len(var) & "
" )
Response.Write( "<u>Value</u></strong>
" )
Response.Write( Replace( var, vbCrLf, "
") )
Else
Dim intDims, i, j
intDims = CountDims( var )
if intDims < 3 then
For i = 0 to UBound( var, 1 )
If intDims = 1 then
Response.Write("var( " & i & " ): " )
Response.Write( typename( var( i ) ) & " = " )
Response.Write( var( i ) & "
" & vbCrLf )
Else
For j = 0 to UBound( var, 2 )
Response.Write("var( " & i & ", " & j & " ): " )
Response.Write( typename( var( i, j ) ) & " = " )
Response.Write( var( i, j ) & "
" & vbCrLf )
Next
End If
Next
Else
Response.Write( “array has “ & intDims & “ dimensions.” )
End If
End If
Response.Write("</span>")
Response.End()
End Sub
For this to work on arrays, you’ll need the function below. Currently the debugger works with up to two dimensions, but could be modified to do more if that’s necessary within your application.
<script language="JScript" runat="server">
function CountDims( arrVBArray ) {
return arrVBArray.dimensions();
}
</script>
Basically, you could use the subprocedure debug() anywhere you want to see what a variable is in gory detail. What I have started doing is inserting it - commented out - in places where I could foresee errors occurring, such as when picking up Query String elements, or with SQL SELECT statements. Example:
Dim intUserID
, strSQL
intUserID = Trim( Request.QueryString(“intUserID”) )
‘debug( intUserID )
strSQL = “SELECT * FROM users WHERE id = “ & intUserID
‘debug( strSQL )
This way, when I need to do some quick debugging, I simply un-comment the desired line, and I’m well on my way to finding the cause of the error.
ConclusionI hope you find these techniques as useful as I have. I can honestly say that using this method of error reporting has saved me hours of frustrated searching. I also believe that using the debugger has prevented carpal tunnel syndrome brought on by typing millions of
Response.Write()- Response.End()’s.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |