"On error" in ASP

Posted on 1999-09-29
Medium Priority
Last Modified: 2013-12-25
Can some please give me an example (code) of using the "on error" statement in ASP

What I want is an email sent to me if an error accurs anywhere on the page. I know how to send an email using ASPMail (or ASPEmail) but how do I trigger the email being sent??
Question by:adinas
  • 2
LVL 25

Accepted Solution

clockwatcher earned 400 total points
ID: 2086292
It's kind of limited in VBScript since you can only use Resume Next, but here's a sample.

sub error_handler(myError, errorCount, errorString)    
  errorCount = errorCount + 1
  errorString = errorString &  "I ran into error #" & myError.number & "-- " & myError.description & "<BR>"
end sub


on error resume next

myErrCount = 0
myErrString = ""

for i = 1 to 10
  d1 = int(rnd * 10)
  d2 = int(rnd * 7)
  response.write d1 & "/" & d2 & " = " 
  response.write d1/d2
  response.write "<BR>"
  if err.number <> 0 then error_handler err, myErrCount, myErrString
<% if myErrCount > 0 then %>
I ran into the following errors: <BR>
<% response.write myErrString
else %>
I didn't hit any errors.
<% end if %>


The problem with resume next is that you kind of have to know where your errors may occur so that you can test the err object.  In most cases, it's fairly obvious which lines are going to cause problems, so it isn't usually that big of a deal to check the Err object.

If you're using ADO, then you should use your Connection's Error collection to pull your errors.  Make a database call, and then check your Errors collection to see if anything went wrong.  If you're not using ADO, just ignore those last comments.

Author Comment

ID: 2097060
Thanks for the answer its working perfectly.

But what if I do want the page to STOP proccessing but I still want the email sent to me? Is there a variation to the "on error resume next" command?

Author Comment

ID: 10852221
What you can do is redirect the user to a standard error page you create when an error happens. something like "Sorry, an error has occurred in the application...". If its for debugging, you will just have to comment the "on error" line when in debugging.

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Ready to get certified? Check out some courses that help you prepare for third-party exams.
The Super Bowl is just days away. Millions of advertising dollars will be spent in just a few hours to drive people to websites around the globe. Optimizing your site in anticipation of a big event like this (and the traffic surges that follow) will…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question