Recommended: ASP Tutorial: Learning ASP scripting programs for the first time Why learn scripting language? What is the relationship between ASP and scripting language? First, let’s talk about what ASP is precious. ASP is the abbreviation of Microsoft Active Server Pages, a server-side scripting environment that can be used to create interactive web pages and build powerful
ASP Error Handling
ASP is so simple that many developers don't think about error handling. Error handling can make your application more reasonable. I've seen many commercial websites written in ASP, most of which ignore error handling.
The wrong type
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 all terminates the operation of the ASP and leaves a bunch of very unfriendly text for the user.
So how do we deal with operation errors! ? Let’s take a look first. The only error command provided to us 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 the On Error Resume Next statement, all running errors will occur, which is fatal, then an error code will be displayed to the user, and the ASP program will also stop.
Here is an error code:
| The following is the quoted content: 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:
| The following is the quoted content: <% '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 'Show 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 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:
| The following is the quoted content: If Err.Number = 0 And objConnection.Errors.Count = 0 Then 'The statement can only be executed here because there are no errors 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 handling both database and page errors. With it, we can discover all the errors in our program at once.
| The following is the quoted content: <% If Err.Number <> 0 Then Response.Clear Select Case Err.Number Case 8 'Specify the wrong number 'Troubleshoot 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 above example allows us to deal with many problems that arise 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:
| The following is the quoted content: If Err.Number = 0 And objConnection.Errors.Count = 0 Then Response.Clear Response.Redirect 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 the On Error Resume Next statement at the top of your program (after the language declaration of course).
Do error checking before you execute SQL.
Error handling was also required before using redirect.
Let you handle the errors containing files at the top of the code.
Share: ASP program realizes the pagination function of saving parameter values The following is the referenced content: <%''' '' Call example'Dim int_RPP, int_Start, int_showNumberLi