Link to home
Start Free TrialLog in
Avatar of greatseats
greatseatsFlag for United States of America

asked on

How to correctly use "On Error Resume Next" in ASP

Hello experts,

Currently I have a custom 500 error page set up in IIS that logs errors.  However whenever an error occurs the page stops processing and the 500 error page is loaded.  I would like to instead use the "On Error Resume Next" method, but I having some problems.

First here is the code from my 500 error page that prints out the error details.  This works correctly - whenever there is a 500 error it prints out the error description, the line, and the column:

Set ASPErr = Server.GetLastError()
response.write("Line: " & ASPErr.Line & "<br>")
response.write("Column: " & ASPErr.Column & "<br>")
response.write("Description: " & ASPErr.Description & "<br>")

Here is the code I am using to implement "On error resume next".  This code is not working.

On Error Resume Next
'database request
If Err.number <> 0 then
    Set ASPErr = Server.GetLastError()      
    response.write("Line: " & ASPErr.Line & "<br>")
    response.write("Column: " & ASPErr.Column & "<br>")
    response.write("Description: " & ASPErr.Description & "<br>")
End If

This code does not work correctly.  It always returns 0 as the error line and -1 as the column number, no matter where the error occurs.  It also does not return the description.

Can anyone tell me what I am doing wrong here?  Is it a mistake in my code?  Why does it work correctly on the custom 500 error page but incorrectly when using "On Error Resume Next"?

Thanks in advance!
Avatar of WMIF
WMIF

you shouldnt need the "Set ASPErr = Server.GetLastError()" line in there.  try using the same properties but with the Err object.
Avatar of greatseats

ASKER

WMIF,

I adjusted my code to read:

If Err.number <> 0 then
    response.write("Line: " & Err.Line & "<br>")
    response.write("Column: " & Err.Column & "<br>")
    response.write("Description: " & Err.Description & "<br>")
End If

The output error description now reads:  "Object doesn't support this property or method".  However that error description is not correct; I get that description for any error, be it an SQL error, Type mismatch, Overflow, etc.

I believe that this error description is in reference to the "Err.Line" and "Err.Column" code; If I remove those two items I get the correct error description for whatever went wrong.  Is there a way to output the error line?
what if you write the description property first?
WMIF,

If I write the description property first I get the correct description.  However I get nothing for the  Err.Column and Err.Line values.
Avatar of Chris Dent

Line and Column aren't properties of the Err Object in VbScript. You get:

Description
Source
Number

As well as two methods (Raise and Clear).

This may not help too much as it won't help you find a line number. But that's what you get available to you.

You can find lots more on the Err Object and error handling in general in VbScript here:

http://www.microsoft.com/technet/scriptcenter/resources/scriptshop/shop1205.mspx
http://www.microsoft.com/technet/scriptcenter/resources/scriptshop/shop0106.mspx

Silly titles, but good articles.

Chris
Chris-Dent,

Is there any way for me to get the line number of the error?
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Chris-Dent,

Thanks for the info.  It looks like the hard-coding method is my best bet.  I was just hoping it wouldn't come to that:-).  Thanks again!

No problem, good luck!

Chris