There are three main error types:
Compilation error:
This kind of error occurs generally because of the code syntax problem.
The verb ASP stopped running due to a compilation 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, but it is beyond the scope allowed by the variable.
Logical error
Logical errors are the most difficult to detect. This kind of error is often a structural error that cannot be discovered by a computer.
This requires us to thoroughly check our code.
Because compilation errors usually occur together with logical errors and can generally be displayed, what we are worried about is the operation error. It terminates the operation of the ASP and leaves a bunch of unfriendly text for the user.
So how do we deal with operation errors! ? Let's take a look first. The only error command provided by ASP - On Error Resume Next (I would like to remind beginners that there are only On Error Resume Next statements in ASP, but no On Error Resume Goto statements) If you do not use On Error Resume Next statement, all running errors will occur. This is fatal, and an error code will be displayed 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 on 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 message after an error occurs. But there are also disadvantages in this way, that is, if the program does not execute as you imagine, it will be difficult for you to find out what is wrong, so you have to deal with the errors where necessary.
Handling errors
In ASP, the best way to deal with errors is to put code at the bottom of the program to handle errors. I also recommend using buffers 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 about you! 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 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 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 message: <%= Err.Description %><BR> Error occurred 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 an error will not affect the execution of the program.
Error handling and database
The execution of adding databases to error handling is very complicated. If we have a program with many commands to add records to the database, if insert/update is executed at the bottom of the program, if our error occurs again before, then it will be over! We will add an error message to the database. Because we used On Error Resume Next, all errors were ignored! Even if there is an error before, the program will still add data to the database.
To avoid this situation, we have to do some tricks first. The correct way to deal with it 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. Below is an example of handling both database and page errors. With it, we can discover all the errors in our program at once. (Since I think English is more difficult to speak in some places, there is no translation).
<% If Err.Number <> 0 Then Response.Clear Select Case Err.Number Case 8 'Specify the Error Number 'Case Else here '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 many problems that occur in the database, which is also commonly used in our daily programming! We should also see that 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 often use. If a redirect object appears in a page, then error handling will lose its meaning. So we have to deal with it before turning, as follows:
If Err.Number = 0 And objConnection.Errors.Count = 0 Then Response.Clear Response.Redirect ?lt;URL Here>? End If
Make the code more neat
To make the code more neat, first place the error-handled file in a contain file. This way you can use it in any file. This is also convenient to modify.
Add (after the language declaration of course) the On Error Resume Next statement at the top of your program.
Do error checking before you execute SQL.
Error handling was also required before using redirect.
Let you handle errors containing files at the top of the code