Author: Flying Popularity: 4409
Brief introduction ASP is very simple, so that many developers do not think about incorrectly. Error handling makes your application more reasonable. I have seen many business websites written in ASP, most of them ignore the error handling. There are three main error types:
Compilation error: This error occurs generally the syntax problem of the code. Because the compilation is wrong and the ASP stops running. Running error This error occurs when you are ready to run the ASP. For example: If you try to assign a variable, it exceeds the range allowed by this variable. Logical error logic errors are the most difficult, this error is often a structural error, and the computer is not found. This requires us to completely check our code correctly. Because compiling errors are generally occurring with logical errors, we can generally display it, so we are worried that just run errors. It terminates the operation of the ASP, and gives users a pile of unfriendly text. So how do we have to handle running errors! ? Let's take a look at the only error command to provide us - On Error ResMe next (here alerting beginners, only the ON Error Resume next statement in the ASP, no on error resume goto statement) If you don't use on If the ERROR RESUME NEXT statement, everything will happen, this is fatal, then there will be a paragraph code "show" to the user, and the ASP program will stop. Below 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 program, all errors are ignored and the program will automatically perform the next statement. This makes it fully executed, and the user will not see the error message after the error. But there is a bad place, that is if the program does not follow your imagination, you will be difficult to find out where there is a problem, so you have to handle the error in the necessary place. Handling errors In ASP, the best way to handle errors is to process errors in the bottom of the program. I also recommend that all ASP programs use buffers. In this case, if the error occurs, the page will stop, the page content will be cleared, so the user will not see the error message, which is less for your complaint! Below is an example:
<% @ Language = "VBScript"%>
<% 'Set buffer for true
Response.buffer = TRUE
'Start error handling
ON Error ResMe next
%>
<% 'Error handling
IF Err.Number <> 0 THEN
'Clear page
Response.clear
'Display error message to user%>
HEAD>
an error Occurred in The Execution of this ASP Page
Please report the following information to the support desk
Page error Object b>
Error Number: <% = err.number%>
Error message: <% = err.description%>
Error file: <% = Err.Source%>
Row: <% = err.line%>
Font>
Body>
Html>
<% End if%>
I saw it above, I first set the ON Error Resume next, so that there is no error that does not affect the execution of the program. Error handling and database execution of the database in error handling are complex. If we have a program, there are many commands to add a record to the database. If INSERT / UPDATE executes in the bottom of the program, if we happen in front of us, it is over! We will add an error to the database. Because we used the error in Error Resume next, it was ignored! Even if the previous error, the program still adds data to the database. In order to avoid this, we must first do some hands and feet, the method of correctly processing is as follows:
If Err.Number = 0 and Objconnection.errors.count = 0 THEN
'Here you can perform a statement because there is no error
Set rstresults = dbdata.execute (txtsql)
END IF
More advanced processing Measures When an error occurs, you can also display more error messages. Below is an example in handling databases and page errors, with it, we can find all errors in our program. (Since some places I think English is more problematic, there is no translation).
<%
IF Err.Number <> 0 THEN
Response.clear
Select Case Err.NUMBER
Case 8 'Specifies the wrong Number
'Handling custom error here
Case Else 'General errors
IF isobject (objconnection) THEN
IF Objconnection.errors.count> 0 THEN
%>
Database Connection Object b>
<% For intloop = 0 to objconnection.errors.count - 1%>
Error NO: <% = Objconnection.errors (Intloop) .Number%>
Description: <% = Objconnection.erroS (INTLOOP) .description%>
Source: <% = Objconnection.errors (intloop). Source%>
SqlState: <% = objconnection.erroS (INTLOOP) .sqlstate%>
NativeError: <% = objconnection.errors (intloop) .nativeerror>
<% next
END IF
END IF
IF Err.Number <> 0 THEN
%>
Page Error Object b>
Error Number <% = Err.Number%>
Error Description <% = Err.Description%>
Source <% = Err.Source%>
LINENUMBER <% = err.line%>
<% End if
End SELECT
END IF
%>
The above example made us handled a lot of problems that occur in the database, this is often used in our daily programming! We should also see that SELECT CASE statement that allows us to handle specific errors. REDIRECT and error handling have a little more, it is to pay attention to our usual redirect object. If a redirect object appears in a page, then the error handles lose meaning. So we have to handle it before turning, as follows:
If Err.Number = 0 and Objconnection.errors.count = 0 THEN
Response.clear
Response.Redirect? Lt; url here>?
END IF
Bringing the code more neat in order to make the code more neatly, first put the error handled file in an included file. This way you can use it in any file. This is also convenient to modify it.
Join the top of your program (Of course, after the language declaration) The ON Error Resume next statement. Check before you perform SQL. Error processing should also be conducted before using Redirect. Let you handle the wrong containment file on the top of the code