Brief introduction
ASP is so simple that many developers don’t think about error handling. Correct handling of errors can make your application more reasonable. I've seen many commercial websites written in ASP, most of which ignore error handling.
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 first take a look at the only error command provided to us by ASP - OnErrorResumeNext (I would like to remind beginners here that there is only OnErrorResumeNext statement in ASP, no OnErrorResumeGoto statement). If you do not use OnErrorResumeNext statement, all operation errors will occur. This is fatal, then an error code will be "displayed" to the user, and the ASP program will also stop.
Here is an error code:
Microsoft OLEDB Provider for ODBCDriverserror80004005
[Microsoft][ODBCDriverManager]Datasourcenamenotfoundedandnodefaultdriverspecified
/test.asp,line60
When we use the OnErrorResumeNext 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
OnErrorResumeNext
%>
<%' error handling
IfErr.Number<>0Then
'Clear the page
Response.Clear
'Show error message to the user