Codefixer - ASP tutorials, resources and software
New MS SQL 2008 Available – DiscountASP.NET
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Monday 6 October
MembersPro PayPal - ASP Membership software

New MS SQL 2008 Available – DiscountASP.NET


ASP Partner ASP Partner ASP Partner more partners...

Error Handling

No matter how good a programmer you are you will undoubtedly make mistakes in your code, so its good programming to write code to cope with this eventuality.You can easily trap and turn nasty error messages into easy on the eye messages that the user can understand.

The first thing you need to do is to put the following line on the top of your ASP page or code:

<%
On Error resume Next
%>

This four word combination is used to tell the server to resume the next line of code if it stumbles upon an error, the ASP script will continue to execute instead of throwing up an exception. The following code will display the error number, description and source.

<%
If Err.number <> 0 then     'if there is an error
response.write Err.number     'write out the error number
response.write Err.source     'write out the error source
response.write Err.description     'write out the error description
End If
%>

Here's a version of the above code slightly better formatted;

<%
If Err.number <> 0 then     'if there is an error
response.write ("Sorry you have encoutered an error."  & "<br>")
response.write ("Your error number is " & Err.number  & "<br>")
response.write ("The error source is " & Err.source & "<br>")    
response.write ("The error description is " & Err.description  & "<br>")  
End If
%>

"On Error Resume Next" is usually placed at the top of each page.

Below is an example with all the code coupled together:

<%
On Error Resume Next
respons.write ("there is an error here as response is spelt respons")

If Err.number <> 0 then     'if there is an error
response.write ("Sorry you have encoutered an error."  & "<br />")
response.write ("Your error number is " & Err.number  & "<br />")
response.write ("The error source is " & Err.source & "<br />")    
response.write ("The error description is " & Err.description  & "<br />")  
End If
%>

Even though the code has come across an error there will be no ugly error message displayed and the execution of the code will continue. As there is an error the description, number and source will be written out.

Note that the following two lines of code are the same:

If Err.Number <> 0 Then
If Err <> 0 Then
LinksPro - Directory  and Link  Management Software




ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software