Test printing, validity checking and error handling in ASP programming

Source: Internet
Author: User
Tags define assert error code error handling variables variable versions
Programming | errors | error handling | Printing often see some beginner ASP's friends to test a value everywhere with Response.Write to print,

and to see the page effect and then delete these statements or add comments, before the official version come out so repeatedly

Times. And some people in order to reduce the trouble, simply all when it is correct, do not do test output, like this pole

The most common is that if you want to generate an SQL statement, you need to use variables such as

Do not do test printing, it is difficult to do the right once, in the Chinaasp forum often see this problem, the old

It was someone who asked why the statement was wrong, and in fact he just printed out the statement and read the words.

Whether the law is correct, rather than chasing people around to ask. In fact good programming habits should be in their own not

Have a great grasp of the case to print the resulting statement or variable values, but this is time-consuming and laborious, there

There is no better way to solve it?
A test switch such as _DEBUG can be used in C to control the debug and release versions, but

The ASP does not have similar #define such statement, then is not we have no way? Actually, we

You can follow this practice in C, which is to define a application variable in the Global.asa file to

Control, like the following example:

Add in global.asp:
Application ("DEBUG") = 1

And then do a process like this:
'--------------------------------------------------------
' Name:print
' Argument:a_strprint: Print string
' Return:
' Description: Print (run only in debug state)
' Hitory:create by Bigeagle
'--------------------------------------------------------
Sub PRINT (A_strprint)
If Application ("DEBUG") = 1 Then
Response.Write ("<p aling=center>" +a_strprint+ "</P>")
End If
End Sub

The function of this process is to print when the test switch is turned on (Application ("DEBUG") = 1)

, and when the test switch is turned off (application ("DEBUG") = 0) There will be no print output. Such

During the program debugging, you can open the test switch to observe the value of the variable, and when you want to see the page effect or

You can turn off the test switch when releasing release versions so that all test outputs do not appear on the page

On

The above is about the test output of variables, the following to talk about the correctness of detection. Often see a lot of

The person submits the page to come over or the value which the database takes out does not want to use, does not do the correctness detection at all, that you

How can we guarantee the correctness of these values? For example, there is an input, after the submission of its value should be a value package

A string with numbers, but if the user's input contains other characters, if you don't do the correctness check, then when you

An error occurs when converting with CInt or CLNG, and the entire program crashes. Another situation is this, when you

Taking a value from a database or something like that should not be a problem, but if the database appears

Wrong and so on, the user can only see a message such as ' ODBC error ', and so on, for a

Mature commodity procedures, this is very bad, in fact, now including many well-known domestic sites also appear

This problem. So we should develop the habit that any variables and parameters that may be problematic are

The correctness test should be done before use, and it should be judged if the database operation is successful. This is another version of the problem, if it is the debug version should display error information for modification, and release version should be guided to a unified page, such as "the site temporarily unknown fault, please come back later" and so on, in principle never give users a system error information page. To implement these features, see the following functions and procedures.

'--------------------------------------------------------
' Name:assert
' Argument:a_blnconditon: Assertion condition
' A_functionname: Calling function
' A_errorstring: Error description
' Return:
' Description: Assertion
' Hitory:create by Bigeagle
'--------------------------------------------------------
Sub ASSERT (a_blnconditon,a_functionname,a_errorstring)
If Application ("DEBUG") = 0 Then
If A_blnconditon <> TRUE Then
Response.Redirect (".. /include/bigerror.asp ")
End If
Else
If A_blnconditon <> TRUE Then
Call Print ("Assertion error: in <font color=red>" + a_functionname + "</font> appears:" + a_errorstring)
Response.End

End If

End If

End Sub

The effect of this process is to detect the validity of a variable or parameter, if the condition is a_blncondition<>true, then if the test switch is turned on, an error message is displayed, and if the test switch is turned off, redirect to the error-handling page bigerror.asp.


'--------------------------------------------------------
' Name:checkerror
' Argument:
' Return:
' Description: Check for errors
' Hitory:create by Yaozhigang
'--------------------------------------------------------
Function CheckError ()
Dim Interrnumber
Interrnumber = Err.Number ' Saves the error code because it will execute in error err.clear

If Interrnumber <> 0 Then call error ( -1, "") ' Err error code is-1

CheckError = Interrnumber
End Function


'--------------------------------------------------------
' Name:error
' Argument:a_interrcode: Error code (-1 indicates a system error, i.e. err.number<>0)
' A_strerrtext: Error description
' Return:
' Description: Error handling
' Hitory:create by Yaozhigang
'--------------------------------------------------------
Sub ERROR (A_interrcode, A_strerrtext)

Dim STRMSG
Dim strlogmsg

' If err is wrong, then error page must be executed
If A_interrcode =-1 Then

STRMSG = strMsg + "*********************************************************************************************" + "<BR>"
STRMSG = strMsg + "Error time:" + CStr (now ()) + "<BR>"
STRMSG = strMsg + "Error type: Err error" + "<BR>"
STRMSG = strMsg + "error number:" + CStr (err.number) + "<BR>"



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.