44

What options are there in ASP Classic 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 continue and/or show a message.

Any ideas?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Sander Versluys
  • 72,737
  • 23
  • 84
  • 91

8 Answers8

53

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:

<%
' Turn off 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 ' Turn error handling back on for errors in your handling block
   ' Code to cope with the error here
End If
On Error Goto 0 ' Reset error handling.

%>
Wolfwyrd
  • 15,716
  • 5
  • 47
  • 67
15

Regarding Wolfwyrd's anwer: "On Error Resume Next" in fact turns error handling off! Not on. On Error Goto 0 turns error-handling back ON because at the least, we want the machine to catch it if we didn't write it in ourselves. Off = leaving it to you to handle it.

If you use On Error Resume Next, you need to be careful about how much code you include after it: remember, the phrase "If Err.Number <> 0 Then" only refers to the most previous error triggered.

If your block of code after "On Error Resume Next" has several places where you might reasonably expect it to fail, then you must place "If Err.number <> 0" after each and every one of those possible failure lines, to check execution.

Otherwise, after "on error resume next" means just what it says - your code can fail on as many lines as it likes and execution will continue merrily along. That's why it's a pain in the ass.

Phil Edwards
  • 161
  • 1
  • 4
9

1) Add On Error Resume Next at top of the page

2) Add following code at bottom of the page

If Err.Number <> 0 Then

  Response.Write (Err.Description)   

  Response.End 

End If

On Error GoTo 0
James Skemp
  • 8,018
  • 9
  • 64
  • 107
user2289611
  • 91
  • 1
  • 2
8

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
Svante Svenson
  • 12,315
  • 4
  • 41
  • 45
6

the statement On Error Resume Next should be placed on top of what we want to validate.

  On Error Resume Next
  'Your code logic is here

Then end with statement like:

  If Err.Number <> 0 then

  'Your error message goes here'

  End if
Levi
  • 661
  • 7
  • 26
mrjurin
  • 136
  • 2
  • 8
3

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

Here is a link to Archive.org's version

tisaconundrum
  • 2,156
  • 2
  • 22
  • 37
2

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 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...

Tomalak
  • 332,285
  • 67
  • 532
  • 628
annakata
  • 74,572
  • 17
  • 113
  • 180
  • 2
    Nice to know, but bejeebus thats ugly. I'd switch scripting language. – Binary Worrier Jan 23 '09 at 11:36
  • 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 Jan 23 '09 at 12:46
  • 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 Jan 25 '09 at 23:22
1

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

My preference is definitely 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131