views:

5762

answers:

5

What options are there in asp for error handling.

For example:

I'm using the Mail.SendMail function but when switching on the testing server it doesn't work, which is normal. I want to test if mailing is possible, if not then continu and/or show a message.

Any ideas?

+2  A: 

Been a while since I was in ASP land, but iirc there's a couple of ways:

try catch finally can be reasonably simulated in VBS (good article here) and there's an event called class_terminate you can watch and catch exceptions globally in. Then there's the possibility of changing your scripting language...

annakata
Nice to know, but bejeebus thats ugly. I'd switch scripting language.
Binary Worrier
Tell me about it :-) I've got to maintain some old projects and ASP lacks quite a lot of those standard features other scripting languages have nowadays...
Sander Versluys
try catch finally definitely does _NOT_ exist in VBScript. That article demonstrates a means to achieve the same goal but its hardly reason to describe VBS as actually having try catch finally.
AnthonyWJones
hyperbole: one of my many flaws (edited)
annakata
+8  A: 

There are two approaches, you can code in JScript or VBScript which do have the construct or you can fudge it in your code.

Using JScript you'd use the following type of construct:

<script language="jscript" runat="server">
try {
      tryStatements}
catch(exception){
      catchStatements}
finally {
      finallyStatements}
</script>

In your ASP code you fudge it by using on error resume next at the point you'd have a try and checking err.Number at the point of a catch like:

<%
Dim i
' Turn on error Handling
On Error Resume Next


'Code here that you want to catch errors from

' Error Handler
If Err.Number <> 0 Then
   ' Error Occurred / Trap it
   On Error Goto 0 ' But don't let other errors hide!
   ' Code to cope with the error here
End If
On Error Goto 0 ' Reset error handling.

%>
Wolfwyrd
+1. Javascript. If you need it, forget VBscript use javascript.
AnthonyWJones
+1  A: 

Some scenarios don't always allow developers to switch scripting language.

My preference is definately for Javascript (and I have used it in new projects), however maintaining older projects is still required and necessary. Unfortunately, these are written in VBScript.

So even though this solution doesn't offer true "try/catch" functionaility, the result is the same, and that's good enough for me to get the job done

Turgs
+2  A: 

A rather nice way to handle this for missing COM classes:

Dim o:Set o = Nothing
On Error Resume Next
Set o = CreateObject("foo.bar")
On Error Goto 0
If o Is Nothing Then
  Response.Write "Oups, foo.bar isn't installed on this server!"
Else
  Response.Write "Foo bar found, yay."
End If
svinto
+2  A: 

For anytone who has worked in ASP as well as more modern languages, the question will provoke a chuckle. In my experience using a custom error handler (set up in IIS to handle the 500;100 errors) is the best option for ASP error handling. This article describes the approach and even gives you some sample code / database table definition.

http://www.15seconds.com/issue/020821.htm

Josh Warner-Burke