Blog Categories


Technology
Food
General

Blog Search

Search Terms:

Latest Blog Entries

Most Viewed Blog Entries

RSS Feed

RSS Feed

Web Design and Development


I can provide a wide number of web-related services, including:
  • Complete Web Site Development
  • Layout, Graphic Design, and Logo Creation
  • Perl, ASP, and PHP Programming
  • Custom Scripting and Modifications
  • Internet Marketing and Search Engine Optimization
  • Usability and Accessibility Testing & Debugging
View more info on my web services.











Scott Roberts
1059 Anise Lane
Fenton, MO 63026
scott@scottrobertsweb.com 636-343-1439









American Flag

I support U.S. Troops


Using the Err Object to Trap Errors in Classic ASP

Posted by: Scott Roberts   |   Category: technology
Updated on April 21, 2008 at 6:55 am
It's inevitable that you'll generate an error or two (or three, or four...) when you code in Classic ASP. I'll show you how to trap errors and use them to your advantage.

Before working with the Err Object, it's helpful to know its properties:

PropertyDescriptionExample
NumberError number for the most recent error (helpful for trapping)7
DescriptionString containing a description of the errorDivision by 0
SourceSource of an error, such as an object or applicationProject.class


Code Snytax

First, discover if Err.Number is greater than 0.

If Err.Number > 0 Then
'Place error handling code here, such as description and custom message
End If


Here is an instance of attempting to access a subscript of an array that is out of range, making it invalid:

myArr = Array("one","two","three")
Response.Write("Element #3 is " & myArr(3) & "
")

The result that shows up in your browser window is fairly cold and technical, and doesn't adequately describe to some people what the problem is:

Microsoft VBScript runtime error '800a0009'

Subscript out of range: '[number: 3]'

/login.asp, line 110


We can do better than this. By trapping this error, we can print out a much easier-to-read error message.

myArr = Array("one","two","three")
Response.Write("Element #3 is " & myArr(3) & "
")

If Err.Number > 0 Then
Response.Write("Error #" & Err.Number & "
")
Response.Write("Error Source: " & Err.Source & "
")
Response.Write("Error Description: " & Err.Description & "
")
End If


And here is its output:

Error #7
Error Source: Microsoft VBScript runtime error
Error Description: Subscript out of range


This result, although only tweaked a small bit from the first example, is much simpler to understand. Use these custom error messages whenever you want to help explain the error to yourself or wish to report an error in a nicer fashion to your users. And if you really want to make this more "user-friendly", you can add the custom layout of your website (especially if you have all your repeated page elements in .inc files) to replace the bland, featureless, white pages that normally appear on your browser when an error occurs.




     Comments

    No comments yet

       Add Your Comment:

* Name:
Email:
Notify me about new comments on this page
Hide my email
* Comments: