Recommended: Use ASP to develop web sites Generally, large-scale websites are less and less likely to use ASP for architecture, but ASP still occupies a large market scope in small and medium-sized business sites. ASP is favored by small and medium-sized site administrators for its simple, short development cycle and easy maintenance. However, as far as I know
7.4.2 VBScript Error Handling
In VBScript, the script interpreter can be made not handle any errors it finds and continue running the next statement using the On Error Resume Next statement. Once this statement has been processed, the script engine will continue to run the subsequent program without paying any errors that have been found. However, this process only applies to environments where statements are executed sequentially, and in other words, not to nested functions or subroutines.
1. Use the On Error Resume Next statement
When an error occurs in a subroutine, if the On Error Resume Next statement is not run, the error will be handed over to the environment in which it is called. This process is repeated until the environment running on the On Error Resume Next statement is found, or the default script error handler is found, and the error is handed over to the ASP and IIS displays the default error page.
This wrong call chain means that functions and subroutines can be created that prevent running errors from causing the program to stop running. If an On Error Resume Next statement is placed at the beginning of the subroutine, any run-time errors will abort the run of the subroutine, but the program that calls the subroutine will continue to run without causing the web page to stop.
For example, if you need to write a string to a file, you can access the file through an independent function to prevent errors from interrupting the entire program's running:
' create a file named strFileName, overwriting any existing one with that name
' and writes strContent into it then closes the file
' returns True if it succeeds, or False on any error
Function WriteNewFile(strFileName, strContent)
On Error Resume Next ' turn off the default error handler
WiteNewFile = Flase ' default return value of function
Set objFSO = CreateObject(Scripting.FileSystemObject)
If Err.Number = 0 Then Set objFile = objFSO.CreateTextFile(strFileName, True)
If Err.Number = 0 Then objFile.WriteLine strContent
If Err.Number = 0 Then objFile.Close
If Err.Number = 0 Then WriteNewFile = True
End Function
Note that the above program checks the Number property of the Err object of VBScript before trying to process each program statement. If this value is 0 (no error has occurred yet), the process of filing and creation of the file can be continued. However, if the error does occur, the script engine will set the value of the property of the Err object and continue to process the next line.
As long as it can run normally without causing an error, the return value of the function will be set to True. Otherwise the function will return False. In programming, you can use this function and take other actions after testing it.
Here is a simple example, we want to use a separate function for the first part of the task so that we can more accurately identify where the error occurs. This makes it easier to read the code during debugging. In the main program of the page, three separate functions can be called.
If CreateNewFile(strFileName) Then ' create the new file
Response.Write New file successfully created<BR>
If WriteContent(strContent) Then ' write the content
Response.Write Content written to file<BR>
Else
Response.Write ERROR: Failed to write to the file<BR>
End If
If CloseFile(strFileName) Then
Response.Write File closed<BR>
Else
Response.Write ERROR: Failed to close the file<BR>
End If
Else
Response.Write ERROR: Failed to create the new file<BR>
End Funciotn
2. Using On Error Goto 0
In ASP 2.0 (although no documentation) and ASP 3.0, the default error handling behavior can also be restored using the On Error Goto 0 statement. After running this statement, the run-time error that occurs will cause the default error handling, checking each nested program in the environment chain until the main page code. If there is no other environment to turn off the default error handling, the execution of the web page will stop and display the IIS default error page.
3. VBScript Err Object
In the previous example, when the default error handling is turned off, check whether the error has occurred by checking the Number property of the VBScript Err object. The Err object stores information about runtime errors. Tables 7-3 and 7-4 give the methods and properties provided by the VBScript Err object.
Table 7-3 Methods of VBScript Err object
method
illustrate
Clear
Clear all current Err object settings
Raise
A runtime error occurred
Table 7-4 Properties of VBScript Err object
property
illustrate
Description
Set or return a string describing the error
Number
(Default) Set or return the specified error value
Source
Set or return the name of the object that generated the error
Use these properties to check which error occurs. For example, different measures can be taken based on the error number, or the error information can be provided to the user with the property values of Source and Description, or transferred to a file.
You can also use the Err object to generate an error. Why do these things? Because sometimes I want to send a customized error message to the user. The properties of the Err object can be set to any desired value. Then call the Raise method to generate this error. Doing so will stop the program and pass the error back along the call chain.
The following example shows how to handle errors when reading a text file on the server disk. Note how to use the constant vbObjectError to determine that the selected error number will not be confused with an existing error number. By adding any selected error number to this constant, it is guaranteed not to be confused with predefined errors.
Functoin ReadThisFile(strFileName) ' returns the content as a string
On Error Resume Next
ReadThisFile = ' default return value of function
Set objFSO = CreateObject(Scripting.FileSystemObject)
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
Select Case Err.Number
Case 0 ' OK, take no action
Case 50, 53 ' standard file or path not found errors
' create custom error values and raise error back up the call chain
intErrNumber = vbObjectError 1073 'custom error number
strErrDescript
Share: How to use ASP to obtain the address of the client's real IP? To obtain the client's real IP address through the proxy server, you must use Request.ServerVariables(HTTP_X_FORWARDED_FOR) to read it. But it should be noted that not every proxy server can use Request.ServerVariab