Common ASP error capture methods

Source: Internet
Author: User

ASP is so simple that many developers do not think about error handling. correct error handling can make your application more reasonable. I have seen many commercial websites written in ASP, and most of them ignore error handling.

There are three main error types:
Compilation error:
This error is generally caused by the syntax of the Code.
The ASP program stops running due to a compilation error.

Running Error
This error occurs when you are preparing to run ASP.
For example, if you try to assign a value to a variable, but it is beyond the permitted range of the variable.

Logical error
Logical errors are the most difficult to find. Such errors are often structured errors that cannot be found by computers.
This requires us to thoroughly check our code.
Because compilation errors are generally displayed along with logical errors, we only worry about running errors. It terminates ASP operations and leaves a pile of unfriendly text to users.

So how can we handle running errors !? Let's take a look at it. The only error Command provided by ASP --- On Error resume next (this reminds beginners that there is only the on error resume next statement in ASP, and there is no on error resume GOTO statement) if you do not use the on error resume next statement, all running errors will occur, which is fatal, and an error code will be displayed to the user, the ASP program stops.

The following 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 will be ignored and the program will automatically execute the next statement. In this way, the program will be fully executed, and the user will not see the error information after the error. However, this is also a bad thing, that is, if the program is not executed as expected, it will be hard for you to find out what went wrong, so you have to handle the error where necessary.
 
Handling error
In ASP, the best way to handle errors is to put code at the bottom of the program to handle errors. I also recommend using a buffer in every ASP program. In this way, if an error occurs, the page will stop and the page content will be cleared, so that the user will not see the error message, and there will be fewer complaints! The following 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 the page
Response. Clear
'Display the error message to the user
%>
<HTML>
<Head>
<Title> </title>
</Head>
<Body bgcolor = "# c0c0c0">
<Font face = "Arial"> An error occurred in the execution of this ASP page <br>
Please report 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>
</Font>
</Body>
</Html>
 
<% End if %>
 

As you can see above, I first set on error resume next, so that errors will not affect program execution.
 
Error Handling and database
The execution of adding a database to error handling is complex. If we have a program, there are a lot of commands to add records to the database, if insert/update is executed at the very bottom of the program, if an error occurs before, it will be done! We will add an error message to the database. Because we have used on error resume next, all the errors are ignored! Even if an error occurs, the program will still add data to the database.
To avoid this situation, we have to do some work first. The correct solution is as follows:
 
If err. Number = 0 and objconnection. errors. Count = 0 then
 
'The statement can be executed here because there is no error
Set rstresults = dbdata. Execute (txtsql)
 
End if
 
 
 
More advanced solutions
When an error occurs, you can also display more error messages. The following is an example of simultaneous processing of database and page errors. With this example, we can find all the errors in our program. (In some cases, I think it is a problem to speak English more, so I have not translated it ).
<%
If err. Number <> 0 then
Response. Clear
Select case err. Number
Case 8' specifies the wrong number
'Here to handle custom errors
 
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). nativeerror %> <p>
 
<% Next
End if

End if
If err. Number <> 0 then
%>
 
<B> page error object </B> <br>
Error number <% = err. Number %> <br>
Error description <% = err. Description %> <br>
Source <% = err. Source %> <br>
Linenumber <% = err. Line %> <p>
 
<% End if
End select
End if
%>
 
 
The above example allows us to deal with a lot of problems in the database, which is also common in our daily programming! We should also see the select case statement, which allows us to handle specific errors.
Redirect and error handling
One thing we should pay attention to is the Redirect object we commonly use. If a redirect object appears in a page, the error processing will be meaningless. So we have to deal with it before turning, as shown below:
 
If err. Number = 0 and objconnection. errors. Count = 0 then
 

Response. Clear
Response. Redirect? Lt; Url here>?
 
End if
 
 
 
Code becomes more neat
In order to make the code more neat, first put the file for error handling in an inclusion file. In this way, you can use it in any file. This modification is also convenient.
Add the on error resume next statement at the top of your program (after the language declaration of course.
Perform an error check before you execute the SQL statement.
You must handle errors before using Redirect.
Let you handle errors that contain files at the top of the Code

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.