views:

197

answers:

5

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

A: 
<% On Error Resume Next %>

If Err.number <> 0 then 'do other stuff

Link: http://www.15seconds.com/issue/990603.htm

Kolten
+3  A: 
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
Joost Moesker
Do you mean creating a cDebugger object in every request?
Eduardo Molteni
Indeed. We have two debugging classes in oure app. In production we use one that sends an error report per email, and in development we use one that dumps the debug log to the screen. Switching between development and production is done trough a setting in the application object.
Joost Moesker
A: 

Did you think is possible to create an IHttpModule in .net and attach your module to a web site, just for trap the error on the web site ?

Cédric Boivin
A: 
Eduardo Molteni
A: 

I map the 500 Error Page in IIS to a custom error handling .asp page. This page then uses Server.GetLastError to get the last error send me an email with details about the error, query string, server variables, etc. It then displays a friendly message to the user.

Mike J