Is try-catch like error handling possible in ASP Classic?
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.
%>
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.