Learning | error | error handling | dynamic | Web page
ASP 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:
<%
' Set buffer to True
Response.Buffer = True
' Start error handling
On Error Resume Next
%>
<%
' Error handling
If err.number <> 0 Then
' Clear page
Response.Clear
' Display error message to user
%>
<HTML>
<HEAD>
<TITLE></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.description%><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.
<%
If err.number <> 0 Then
Response.Clear
Select Case Err.Number
Case 8
' Specify 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). 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 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 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.