In classic ASP, is there a way to handle errors at application level

asp-classicerror handling

In classic ASP, is there a way to handle error at application level?

Is there guidelines for handling error / exceptions in classic ASP 3 ? The Server.GetLastError() not a lot to work with…

I am looking for something like the Application_Error() found in an ASP.Net Global.asax.

Any equivalent in a global.asa ? Classes to intelligently log the error ? Like an old Enterprise library exception handling for ASP3…

Hey, I am a dreamer !

Thanks a lot for any pointers

Best Answer

try:
    some code that can raise an error
except:
    do error handeling stuff
finally:
    clean up, close files etc

Can be emulated in vbscript as follows:

class CustomErrorHandler
    private sub class_terminate
        if err.number > 0 then
            do error handeling stuff
        end if 
        clean up, close files etc
    end sub
end class    


with new CustomErrorHandler
    some code
    ...
end with    

How does this work? The 'class_terminate' method will be called when the newly created instance goes out of scope. This happens either when the interperter hits the 'end with' statement or when the callstack gets unwinded due to an error. It's less pretty then the native python approach but it works quite well and isn't to ugly.

For top level error handling you can use the same technique. This time don't use the with statement but create a global instance of your error handler. Beware that the ASPError object provided server.getLastError() isn't the same as the vbscript err object and is only available after IIS has done its server.transfer to the 500:100 error handler and has come back to your page to collect the garbage. Example handler:

class cDebugger
  public sub do_debug
     ' print debug data here
  end sub
  public sub class_terminate
    if server.getlasterror().Number <> 0 then
        response.clear
        call do_debug
    end if
  end sub
end class
Related Topic