How to Handle a Recoverable Error in Classic ASP
VBScript uses inline error handling for recoverable failures. On Error Resume Next continues execution after an error, so it must be followed by an explicit check. Leaving it active across a whole page can turn a failed operation into an apparently successful response.
Isolate one failing operation
Place this in a Classic ASP test page:
<%@ Language="VBScript" %>
<% Option Explicit %>
<%
Dim value, errorNumber, errorSource
On Error Resume Next
Err.Clear
value = CInt("not-a-number")
errorNumber = Err.Number
errorSource = Err.Source
Err.Clear
On Error GoTo 0
If errorNumber <> 0 Then
Response.Status = "400 Bad Request"
Response.ContentType = "text/plain"
Response.Write "The value must be a whole number."
Else
Response.ContentType = "text/plain"
Response.Write "Parsed value: " & CStr(value)
End If
%>
The deliberate conversion failure produces a 400 response and the controlled message. Change the input to "42". It produces Parsed value: 42 with the normal success status. Confirm the status in the browser's Network panel, because IIS custom-error settings can replace an error body.
Preserve the error before doing other work
Read Err.Number, Err.Source and any needed description immediately after the operation. Subsequent COM or script calls can change that information. Log detailed diagnostics on the server, while returning a message appropriate for the caller.
On Error GoTo 0 disables the local handler. It is not a jump to a numbered label. VBScript does not provide the label-based exception handler found in other Visual Basic variants.
Correctly raise an application error
Use Err.Raise number, source, description, for example Err.Raise 1001, "Import", "Missing required field". Do not assign Err.Number and then call Err.Raise without its required number. Compare numbers with <> 0, not just > 0, because some COM error numbers are negative.
References


Author: Herman Schoenfeld