ASP error Handling

Source: Internet
Author: User
Tags add object end error code error handling execution odbc variable
Errors | Error Handling | Simple Introduction to error handling
ASPs are so simple that many developers don't think about error handling. Error handling can make your application more reasonable. I've seen many commercial Web sites written in ASP, most of them ignoring error handling.
Type of error
There are three major types of errors:
Compilation Error:
This error is generally a grammatical problem of the code. The resignation ASP is stopped because of a compile error.
Run error
This error occurs when you are ready to run the ASP. For example, if you try to assign a value to a variable, it exceeds the allowable range for that variable.
Logical error
Logic errors are the hardest to find, and they are often a structural error that computers cannot find. This requires us to thoroughly examine our code.
Because compilation errors usually occur with logical errors, they are generally displayed, so we are worried about just running errors. It terminates the operation of the ASP and throws a bunch of unfriendly text to the user.
So how do we deal with running errors!? Let's take a look at the error command that the ASP only provides to us---on Resume next (here is a reminder for beginners, only on Error Resume Next statement in ASP, no on Error Resume goto statement)
If you do not use the On Error Resume Next statement, all run errors will occur, this is fatal, then there will be an error code "show" to the user, and the ASP program will also stop.

Here is an error code:

Microsoft OLE DB Provider for ODBC Drivers error 80004005
[Microsoft] [ODBC Driver Manager] Data source name not found and no default driver specified
/test.asp, line 60

When we use the On Error Resume Next statement at the top of the program, all errors are ignored and the program automatically executes the next statement. The program will execute completely, and the user will not see the error message after the error. But there's also a downside, which is that if the program doesn't execute as you think, it's hard to find out exactly where the problem is, so you have to deal with the error where necessary.
Handling Errors
In ASP, the best way to handle errors is to put code at the bottom of the program to handle the error. I also recommend using buffers in every ASP program. In this case, if the error occurs, the page will stop, the page content will also be cleared, so that users will not see the error message, you have less complaints! Here is an example:
<%@ language= "VBScript"%>
<% ' Set buffer to True
Response.Buffer = True
' Start error handling
On Error Resume Next
<% ' ERROR handling
If err.number <> 0 Then
' Clear page
' Display error message to user
<body bgcolor= "#C0C0C0" >
<font face= "ARIAL" >an error occurred in the execution of this ASP page<br>
Please have the following information to the support desk<p>
<b>page Error object</b><br>
Error number: <%= Err.Number%><br>
Error message: <%= err.description%><br>
Error file: <%= err.source%><br>
Error line: <%= err.line%><br>

<%end if%>

As you can see above, I set the On error Resume Next so that an error does not affect the execution of the program.

Error handling and Database
It is very complicated to join the database execution in error handling. If we have a program, there are a lot of commands to the database to add records, if the insert/update at the bottom of the program execution, if we are in front of the error occurred, it is over! We will add an error message to the database. Because we used the On Error Resume Next all the mistakes are ignored! The program will still add data to the database even if the previous error occurs.
In order to avoid this situation, we have to do something first, the correct way to deal with the following:

If Err.Number = 0 and ObjConnection.Errors.Count = 0 Then

' Here to execute the statement, because there is no error
Set rstresults = Dbdata.execute (txtsql)

End If

More advanced approaches
When an error occurs, you can also display more error messages. Here is an example of both database and page errors, with which we can find all the bugs in our program. (because there are some places I think English is more able to speak the problem, so there is no translation).
If err.number <> 0 Then
Select Case Err.Number
Case 8 ' Specifies the wrong number
' Handle Custom errors here

Case Else ' general error

If IsObject (objconnection) Then
If objConnection.Errors.Count > 0 Then

<b>database Connection object</b>

<% for intloop = 0 to Objconnection.errors.count-1%>

Error No: <%= objconnection.errors (intloop). Number%><br>
Description: <%= objconnection.errors (intloop). Description%><br>
Source: <%= objconnection.errors (intloop). Source%><br>
SQLState: <%= objconnection.errors (intloop). SQLState%><br>
NativeError: <%= objconnection.errors (intloop)

[1] [2] Next page

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.