Beginner | mistakes | error Handling | tips
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:
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 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'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:
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= "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
%>
an error occurred in the execution of this ASP page
Please have the following information to the support desk
Page Error Object
Error number: <%= err.number%>
Error message: <%= err.des cription%>
Error file: <%= err.source%>
Error line: <%= err.line%>
<%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 example above lets us handle a lot of problems in the database, which is commonly used in our daily programming! 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 will be useful to you because I have found someone asking this question in our forum! Maybe I can compile this article to some effect on you.