IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
ASP Classic
Search ASP Classic Group:

Advanced
-Collapse +Expand ASP Classic Store

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBWebsite Scri...ASP ClassicLanguage Basics   Print This     
  From the January 2009 Issue of Prestwood eMag
 
ASP Classic Language Basics:
Using On Error Resume Next
 
Posted 18 years ago on 11/15/2006 and updated 7/2/2008
Take Away: You can use "On Error Resume Next" to suppress errors and "On Error Goto 0" to stop suppressing errors.

KB100411

ASP's ability to trap for errors is pretty weak compared to other languages. However, you can trap for errors, suppress errors, and stop suppressing errors. When an error is found, you can also handle it.

Suppressing Errors

To start trapping errors, add the following line:
On Error Resume Next
This will suppress all errors for the duration of the script. For example, normally the following code would cause the death of your code with a division by error message:
Dim x
x = 1/0
However, you can suppress the error as follows:
On Error Resume Next
Dim x
x = 1/0
The above code will cause NO error at all.

Capture Error But Keep Going

While trapping for errors, you can handle the error by inspecting the Err object. The following code snippet handles our division by zero error by displaying a message but the script keeps going:

On Error Resume Next
Dim x
x = 1/0
If Err.Number <> 0 Then
  Response.Write "Error: " & Err.Description
End If
Response.Write "Note, this line here still executes and prints this text."

Stop Suppressing Errors

After calling On Error Resume Next, you can stop suppressing errors with the following code snippet:
On Error Goto 0

Scope of the Err Object

The scope of the Err object exists ONLY until you call On Error Goto 0. For example, the following code snippet does NOT display the Err.Description text:

On Error Resume Next
 Dim x
 x = 1/0
On Error Goto 0
''' Err Object no longer exists! ''' 
If Err.Number <> 0 Then
 '''The following line does NOT print. '''
 Response.Write "Error: " & Err.Description
End If

Using On Error for Debugging

You can use On Error for debugging. The following example illustrates. The following line of code will fail if passed a null value. It may work in testing if your CurrentActualDate field always has a value.
Response.Write FormatDateTime(f_CurrentActualDate, vbShortDate)
Here is how to use On Error to selectively display the value of this variable but only when FormatDateTime fails.
On Error Resume Next
  Response.Write FormatDateTime(f_CurrentActualDate, vbShortDate)
  If ErrNumber <> 0 Then
     Break(f_CurrentActualDate)
  End If
       
On Error Goto 0

Err Object Scope Is NOT Limited to a Method

Despite what I've read recently on some message boards, you can call On Error Resume Next from within a function or subroutine. For example, the following code snippet DOES display our division by zero error:

MyErrorSub
If Err.Number <> 0 Then
 '''HandleError Err.Description
 Response.Write "Error: " & Err.Description
End If
Sub MyErrorSub
 On Error Resume Next
 Dim x
 x = 1/0
End Sub

Make CDO.Send More Robust

On a website hosting server I was using recently, the CDO.Send method was failing sometimes because of a capacity issue the vendor was having. I needed a way to trap for failure and try again. You can use On Error Resume Next to trap for and retry sending of an email using CDO.

  On Error Resume Next
   objCDOMail.Send
   
   If err.number = 0 Then
    SU_SetActionMessage("Email sent....")
   Else
    SU_SetActionMessage("Error: Email did NOT send. Try again.")
   End If
  On Error GoTo 0
Here is the complete example I settled on:
  On Error Resume Next
   Dim Counter
   Counter = 1
   While Counter < 10
      objCDOMail.Send
  
      If Err.Number = 0 Then
       If Counter = 1 Then
       Response.Write("Email sent...")
       Else
      Response.Write("Email sent...   (Attempts needed: " & Counter & ")")
     End If
     Counter = 10
      Else
     Counter = Counter + 1
     If Counter => 10 Then
      Resonse.Write("Email NOT sent (10 attempts failed). If important, try again.")
     End If
      End If
   WEnd
  On Error GoTo 0

Comments

1 Comments.
Share a thought or comment...
Comment 1 of 2

Your code doesnt have Next in it so how does it know where Next is?

---
Anon
Posted 13 years ago

Comment 2 of 2

Maybe I'm dense, but I don't see what "Next" is missing. Which code snippet are you referring to?

Posted 13 years ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1116A1
Enter key:
KB Post Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #100411 Counter
39259
Since 4/2/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]