❗ ASP – Error Handling – Manage and Debug Runtime Errors in Classic ASP
🧲 Introduction – Why Error Handling Matters in ASP?
In Classic ASP, runtime errors such as divide-by-zero, null references, or failed database queries can crash your application if left unhandled. Error handling is essential to gracefully detect, log, and respond to errors without exposing sensitive information to end users.
🎯 In this guide, you’ll learn:
- How to use
On Error Resume Next
and theErr
object - Ways to log and display custom error messages
- Techniques to prevent common ASP errors
- Examples for real-world error detection and handling
🚦 Enabling Error Handling with On Error Resume Next
<%
On Error Resume Next
' Code that may cause an error
Dim x
x = 1 / 0 ' Division by zero
If Err.Number <> 0 Then
Response.Write "Error occurred: " & Err.Description
Err.Clear
End If
%>
🧪 Output:Error occurred: Division by zero
🛠️ The Err
Object – Inspecting Errors
Property | Description |
---|---|
Err.Number | Error code (0 = no error) |
Err.Description | Human-readable error message |
Err.Source | Source of the error |
Err.Clear() | Resets the error state |
📄 Example – Error Logging to File
<%
On Error Resume Next
' Simulate an error
Dim result
result = 10 / 0
If Err.Number <> 0 Then
Dim fso, file
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile(Server.MapPath("error_log.txt"), 8, True)
file.WriteLine Now() & " - " & Err.Description
file.Close
Set file = Nothing
Set fso = Nothing
Err.Clear
End If
%>
🧪 This writes the error to error_log.txt
.
❗ Use Case – Preventing Crash on Missing Query Parameter
<%
On Error Resume Next
Dim id
id = Request.QueryString("id")
If IsNumeric(id) Then
Response.Write "Product ID: " & id
Else
Response.Write "Invalid product ID."
End If
%>
🔁 Custom Error Messages for Users
<%
On Error Resume Next
' Problematic code
Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "invalid-connection-string"
If Err.Number <> 0 Then
Response.Write "<p>We’re experiencing technical difficulties. Please try again later.</p>"
' Log details, hide from user
Err.Clear
End If
%>
🧩 Server.GetLastError (ASP 3.0+)
<%
On Error Resume Next
Dim test
test = 5 / 0
Dim lastError
Set lastError = Server.GetLastError()
If Not lastError Is Nothing Then
Response.Write "Runtime Error: " & lastError.Description
End If
%>
📌 This provides detailed error info without using Err
directly.
🧘 Best Practices for Error Handling
✅ Do:
- Use
On Error Resume Next
sparingly and locally - Always check
Err.Number
after risky operations - Log errors for diagnostics without exposing to users
- Clear errors with
Err.Clear
once handled
❌ Avoid:
- Using global
On Error Resume Next
without checks - Showing raw error details to end users
- Ignoring errors during database or file operations
📌 Summary – Recap & Next Steps
Proper error handling in Classic ASP is critical for stability, security, and user experience. By using Err
, On Error Resume Next
, and logging strategies, you can gracefully manage failures and keep your app running smoothly.
🔍 Key Takeaways:
- Use
On Error Resume Next
+Err
to capture runtime errors - Log errors to a file or database for admin visibility
- Show custom messages to users without exposing details
⚙️ Real-world Use Cases:
- Prevent broken pages from missing form fields
- Safely handle file or database connection failures
- Log ASP page crashes for developer review
❓ FAQs – ASP Error Handling
❓ What does On Error Resume Next
do?
✅ It tells ASP to continue running the script even if an error occurs, allowing you to handle the error manually with Err
.
❓ How can I log errors in ASP?
✅ Use FileSystemObject
with Server.MapPath()
to write logs to a .txt
file.
❓ What’s the difference between Err.Number
and Server.GetLastError()
?
✅ Err.Number
is used in all ASP versions, while Server.GetLastError()
is a newer method introduced in ASP 3.0+ for detailed error objects.
Share Now :