The source of explicit program anomaly ASP error handling summary

Source: Internet
Author: User
Tags case statement count driver manager error code error handling execution odbc tidy
ASP is very simple, so many of the DevelopmentPeople don't think about error handling. Error handling can make your application more reasonable. I've seen a lot of business written in ASP. website, most of them ignore error handling.

Type of error

There are three major types of errors:

1. Compilation Error:

This error is generally a grammatical problem of the code. The resignation ASP is stopped because of a compile error.

2. 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.

3. Logical error:

Logic errors are the hardest to be found, this error is often a structural error, the computer can not 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's a reminder for beginners, in ASP, only on Error Resume Next statement, no On Error Resume goto statement) If you don't use on Error Resume Next statement, all running 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:

   MicrosoftOLE DB Provider for ODBC Drivers error 80004005 [Microsoft][odbc Driver Manager] Data source name not found and no Defau LT 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= "VBs cript"%>
<%response.buffer = True
"Set Buffer to True
On Error Resume Next
"Start error handling
%>
<% "Error Handling
If err.number <> 0 Then
The clear page
Response.Clear
"Displays the error message to the user
%>
<HTML>
<HEAD>
<t ITle> </TITLE>
</HEAD>
<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.des cription%> <BR>
Error file: <%= err.source%> <BR>
Error line: <%= err.line%> <BR>
</FONT>
</BODY>
</HTML>
<%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
Response.Clear
Select Case Err.Number
Case 8
"Specifies the number of the error
"Handles 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>
Des cription: <%= objconnection.errors (intloop). Des cription%> <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 Des cription <%= err.des cription%> <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 that arise in the database, which in our daily ProgrammingIt's also a common use! We should also see the Select Case statement, which allows us to handle a particular error.
Redirect and error handling

One thing we should pay attention to is that we often use the redirect object, if a page appears in the redirect object, then error handling will lose its meaning. So we have to deal with it before we turn around, as follows:

If Err.Number = 0 and ObjConnection.Errors.Count = 0 Then
Response.Clear
Response.Redirect? lt; URL here>?
End If

To get the code more tidy

To make the code more tidy, first put the error-handling file in a containing file. So you can use it in any file. This modification is also convenient. Add at the top of your program (of course after the language Declaration) on the Error Resume Next statement. Check for errors before you execute SQL. Error handling is also done before using redirect.

Let you handle the wrong inclusion file at the top of the code

I hope this article is useful to you because I am in our ForumAlready found someone asking the question! Maybe I can compile this article to some effect on you.


Related Article

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.