ASP skills: knowledge about ASP Error objects

Source: Internet
Author: User
Tags servervariables domain server

In VBScript, there is an On Error Resume Next statement, which causes the script interpreter to ignore runtime errors and continue script code execution. Then the script can check the value of the Err. Number attribute to determine whether an error has occurred. If an error occurs, a non-zero value is returned. In ASP 3.0, you can also use On Error Goto 0 to "return to" default Error handling. This type of processing is actually performed in ASP 2.0, but there is no relevant documentation, which is common in many asp data-related processing files. In addition, with On Error Resume Next, the default Error processing is disabled, then use err to capture,

If Err Then
Err. Clear
Response. Write "error! "
Response. End
End If

For more detailed error description, let's try the asperror object, which is a new object of asp3.0.Getlasterror of the server ObjectThe asperror providesLastThe error details are different from the Err Object of VBScript. You cannot call this method at any time to check whether an error occurs. It can only be used in an ASP-customized error webpage. If you disable the default Error handling (using the On Error Resume Next statement) as you perform operations On the Err Object, The GetLastError method cannot access detailed Error data.
Attributes of the ASPError object:
The ASPError object provides nine attributes to describe the nature and error source of the error and return the actual code that triggers the error. The attributes and descriptions are as follows:

ASPCode: integer. Error code generated by ASP/IIS, for example, 0x800A009
ASPDescription: string type. If this error is an ASP-related error, this attribute is a detailed description of the error. example: All HTTP: HTTP_ACCEPT: */* HTTP_ACCEPT_LANGUAGE: zh-cn HTTP_CONNECTION: Keep-Alive HTTP_HOST: s HTTP_USER_AGENT: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; (R1 1.5 ))... there are also cookies and other reports.
Category: string type. Error source, that is, ASP internal scripting language, or an object.
Column: integer. The character location in the file that generated the error
Description: string type. Brief error description
File: string type. Name of the file being processed when an error occurs
Line: integer. Generate the row number in the wrong file
Number: integer. A standard COM error code
Source: string type. Actual code of the row that causes the error

OK. These are nine attributes. The syntax for using the asperror object is:

Asperror. property
This is the case: ASPError. ASPCode ()
ASPError. ASPDescription ()
ASPError. Category ()
ASPError. Column ()
ASPError. Description ()
ASPError. File ()
ASPError. Line ()
ASPError. Number ()
ASPError. Source ()

When an ASP-related error occurs under any directory supported by iis (or in the directory where the error ing attribute is edited), the custom error page is loaded. This file contains a client script code section. It obtains the URL of the current document (retrieved from the url attribute of the document Object) and displays: [html] <! Doctype html public "-// W3C // dtd html 3.2 Final // EN">
<Html dir = ltr>

<Head>
<Style> a: link {font: 9pt/11pt; color: FF0000} a: visited {font: 9pt/11pt; color: # 4e4e4e}
</Style>

<Meta name = "ROBOTS" CONTENT = "NOINDEX">

<Title> the webpage cannot be found. </title>

<META HTTP-EQUIV = "Content-Type" Content = "text-html; charset = gb2312">
<Meta name = "MS. LOCALE" CONTENT = "ZH-CN">
</Head>

<Script>
Function Homepage (){
<! --
// In real bits, urls get returned to our script like this:
// Res: // shdocvw. dll/http_404.htm # http://www.DocURL.com/bar.htm

// For testing use DocURL = "res: // shdocvw. dll/http_404.htm # https://www.microsoft.com/bar.htm"
DocURL = document. URL;

// This is where the http or https will be, as found by searching for: // but skipping the res ://
ProtocolIndex = DocURL. indexOf (": //", 4 );

// This finds the ending slash for the domain server
ServerIndex = DocURL. indexOf ("/", protocolIndex + 3 );

// For the href, we need a valid URL to the domain. We search for the # symbol to find the begining
// Of the true URL, and add 1 to skip it-this is the BeginURL value. We use serverIndex as the end marker.
// Urlresult = DocURL. substring (protocolIndex-4, serverIndex );
BeginURL = DocURL. indexOf ("#", 1) + 1;

Urlresult = DocURL. substring (BeginURL, serverIndex );

// For display, we need to skip after http: //, and go to the next slash
Displayresult = DocURL. substring (protocolIndex + 3, serverIndex );

InsertElementAnchor (urlresult, displayresult );
}

Function HtmlEncode (text)
{
Return text. replace (// g, '& amp '). replace (/'/g,' & quot ;'). replace (/</g, '& lt ;'). replace (/>/g, '& gt ;');
}

Function TagAttrib (name, value)
{
Return ''+ name + '="' + HtmlEncode (value) + '"';
}

Function PrintTag (tagName, needCloseTag, attrib, inner ){
Document. write ('<' + tagName + attrib + '>' + HtmlEncode (inner ));
If (needCloseTag) document. write ('</' + tagName + '> ');
}

Function URI (href)
{
IEVer = window. navigator. appVersion;
IEVer = IEVer. substr (IEVer. indexOf ('msie ') + 5, 3 );

Return (IEVer. charAt (1) = '.' & IEVer> = '5. 5 ')?
EncodeURI (href ):
Escape (href). replace (/% 3A/g, ':'). replace (/% 3B/g ,';');
}

Function InsertElementAnchor (href, text)
{
PrintTag ('A', true, TagAttrib ('href ', URI (HREF), text );
}

// -->
</Script>

<Body bgcolor = "FFFFFF">

<Table width = "410" cellpadding = "3" cellspacing = "5">

<Tr>
<Td align = "left" valign = "middle" width = "360">
<H1 style = "COLOR: 000000; FONT: 12pt/15pt "> <! -- Problem --> the webpage cannot be found </Td>
</Tr>

<Tr>
<Td width = "400" colspan = "2"> <font style = "COLOR: 000000; FONT: 9pt/11pt "> the webpage you are searching for may have been deleted, renamed, or temporarily unavailable. </Font> </td>
</Tr>

<Tr>
<Td width = "400" colspan = "2"> <font style = "COLOR: 000000; FONT: 9pt/11pt ">

<Hr color = "# C0C0C0" noshade>

<P> perform the following operations: </p>

<Ul>
<Li> if you type a webpage address in the address column, check whether it is correctly spelled. <Br>
</Li>

<Li> open <script>
<! --
If (! (Window. navigator. userAgent. indexOf ("MSIE")> 0) & (window. navigator. appVersion. charAt (0) = "2 ")))
{
Homepage ();
}
// -->
</Script> on the homepage, find a link to the desired information. </Li>

<Li> click <a href = "javascript: history. back (1)"> back </a> to try other links. </Li>
</Ul>

<H2 style = "font: 9pt/11pt; color: 000000"> HTTP 404-files cannot be found <br> Internet Information Service <BR>

<Hr color = "# C0C0C0" noshade>

<P> technical information (personal support) </p>

<Ul>
<Li> details: <br> <a href = "http://www.microsoft.com/ContentRedirect.asp? Prd = iis & BMI = & pver = 5.0 & pid = & ID = 404 & cat = web & OS = & over = & hrd = & Opt1 = & Opt2 = & Opt3 =" target = "_ blank"> Microsoft support </a>
</Li>
</Ul>

</Font> </td>
</Tr>

</Table>
</Body>
</Html>
[/Html]

When an error occurs, the ing between the error and the error webpage file is determined in the Custom Errors tab of the properties dialog box of each directory, which is set in the properties of Internet Services Manager, if anyone is interested, check it out.
The custom error webpage displays all the values of the ASPError object attribute, and returns an HTTP header Status message to the client by using the Response. Status method, indicating that an error has occurred. Then, use the GetLastError method to obtain a reference to the ASPError object. Therefore, you can access detailed error data:
...

<%
Response. Status = "500 Internal Server Error"
Set objASPError = Server. GetLastError ()
%>
Currently executing the page: <B> show_error.asp </B> <P>
<B> Error Details: </B> <BR>

ASPError. ASPCode = <% = objASPError. ASPCode %> <BR>
ASPError. Number = <% = objASPError. Number %> (0x <% = Hex (objASPError. Number) %>) <BR>
ASPError. Source = <% = Server. HTMLEncode (objASPError. Source) %> <BR>
ASPError. Category = <% = objASPError. Category %> <BR>
ASPError. File = <% = objASPError. File %> <BR>
ASPError. Line = <% = objASPError. Line %> <BR>
ASPError. Column = <% = objASPError. Column %> <BR>
ASPError. Description = <% = objASPError. Description %> <BR>
ASPError. ASPDescription = <% = objASPError. ASPDescription %>

<Form action = "<% = Request. ServerVariables (" HTTP_REFERER ") %>" METHOD = "POST">
<Input type = "SUBMIT" NAME = "cmdOK" VALUE = "Return to the previous page">
<P>
</FORM>

If a script or ASP error occurs on a custom error webpage, IIS returns only a general message corresponding to error code 500: 100. This may be an Error message of the script engine, or a fairly simple message: "Internal Server Error ". The custom error webpage will not be reloaded.
All environments containing the wrong webpage will be sent to the custom error webpage. That is to say, you can use the values stored in any ASP internal object set or attribute. For example, if you retrieve the HTTP_REFERER value from the Request. ServerVariables set, it will reflect the URL that calls the original webpage (that is, the webpage before the error occurs. This value will not change when the server transfers the execution to the error webpage, and it will not contain the URL of the webpage being executed when the error occurs.
Similarly, the SCRIPT_NAME value will contain the name of the wrong webpage, rather than the URL of the wrong webpage. When an error webpage has been loaded, check the URL in the address bar of the browser to confirm it. However, the values stored in the original webpage's script variables are unavailable in custom error webpages.
If the original ASP Web page is running in a TRANSACTION, there is a <% @ TRANSACTION = "..." %> Command, you should also determine whether you need to take some methods in the web page to exit the transaction. For example, you can call the SetAbort method of the built-in ObjectContext object:
ObjectContext. SetAbort
 
Well, thousands of spam mails have been sent at the front end, and all of them are handled incorrectly.

Option Explicit
Response. AddHeader "Status Code", "200"
Response. AddHeader "Reason", "OK"
On Error Resume Next
Response. Clear
Dim objError
Set objError = Server. GetLastError ()
 
Dim objErr, objMail, html
Set objErr = Server. GetLastError ()
Set objMail = CreateObject ("CDONTS. NewMail ")
ObjMail. From = "s1z2d3s1@163.com"
ObjMail. to = "5do8@5do8.com"
ObjMail. BodyFormat = 0
ObjMail. MailFormat = 0
ObjMail. Subject = "QOP Error 500"
Html = "<font face = 'verdana, Arial, Helvetica, sans-serif'> <br>"
Html = html & "<p> Error occured at:" & now
Html = html & "<p> Referred from:" & request. ServerVariables ("HTTP_REFERER ")
Html = html & "<p> Url:" & request. ServerVariables ("URL ")
Html = html & "<p> <B> Category: </B> </p>" & objErr. Category
Html = html & "<p> <B> Filename: </B> </p>" & objErr. File
Html = html & "<p> <B> ASP Code: </B> </p>" & objErr. ASPCode
Html = html & "<p> <B> Number: </B> </p>" & objErr. Number
Html = html & "<p> <B> Source: </B> </p>" & objErr. Source
Html = html & "<p> <B> LineNumber: </B> </p>" & objErr. Line
Html = html & "<p> <B> Column: </B> </p>" & objErr. Column
Html = html & "<p> <B> Description: </B> </p>" & objErr. Description
Html = html & "<p> <B> ASP Description: </B> </p>" & objErr. ASPDescription
Html = html & "<blockquote>"
Html = html & "All HTTP:" & Request. ServerVariables ("ALL_HTTP ")
Html = html & "</blockquote> </font>"
ObjMail. Body = html
ObjMail. Send
ObjErr. clear
Set objMail = Nothing
Set objErr = Nothing
Response. write (html)

This operation is really annoying. Let's take a look at what the old guy wrote in 500-100.asp:

<%
Response. Write objASPError. Category
If objASPError. ASPCode> "" Then Response. Write "," & objASPError. ASPCode
Response. Write "(0x" & Hex (objASPError. Number) & ")" & "<br>"

Response. Write "<B>" & objASPError. Description & "</B> <br>"

If objASPError. ASPDescription> "Then Response. Write objASPError. ASPDescription &" <br>"

BlnErrorWritten = False

'Only show the Source if it is available and the request is from the same machine as IIS
If objASPError. Source> "" Then
StrServername = LCase (Request. ServerVariables ("SERVER_NAME "))
StrServerIP = Request. ServerVariables ("LOCAL_ADDR ")
StrRemoteIP = Request. ServerVariables ("REMOTE_ADDR ")
If (strServername = "localhost" Or strServerIP = strRemoteIP) And objASPError. File <> "? "Then
Response. Write objASPError. File
If objASPError. Line> 0 Then Response. Write ", line" & objASPError. Line
If objASPError. Column> 0 Then Response. Write ", column" & objASPError. Column
Response. Write "<br>"
Response. Write "<font style =" "COLOR: 000000; FONT: 8pt/11pt courier new" "> <B>"
Response. Write Server. HTMLEncode (objASPError. Source) & "<br>"
If objASPError. Column> 0 Then Response. Write String (objASPError. Column-1), "-") & "^ <br>"
Response. Write "</B> </font>"
BlnErrorWritten = True
End If
End If

If Not blnErrorWritten And objASPError. File <> "? "Then
Response. Write "<B>" & objASPError. File
If objASPError. Line> 0 Then Response. Write ", line" & objASPError. Line
If objASPError. Column> 0 Then Response. Write ", column" & objASPError. Column
Response. Write "</B> <br>"
End If
%>

For more information about the attributes of the ASPError object, see ASP 3.0 advanced programming:
· Even if no error occurs, the Number attribute should always have a value. If no error occurs when ASP Web page calls the GetLastError method, the value of this attribute is 0. Generally, for an ASP script runtime error, the Number attribute returns the hexadecimal value "0x800A0000" and the standard script engine error code. For example, in the previous example, the returned value of the "Subscript out of Range" error is "0x800A0009", because VBScript has the error code "9" for this type of error ".
· When an error has occurred, the Category and Description attributes always have a value.
· The value of the APSCode attribute is generated by IIS and is null for most script errors. In more cases, when an error occurs when an external component is used, the corresponding value is returned.
· The value of the ASPDescription attribute is generated by the ASP Preprocessing Program, rather than by the script engine currently in use, and will be blank for most script errors. In more cases, there are corresponding values for errors such as invalid methods for ASP built-in object calls.
· The File, Source, Line, and column attributes can be set only when an error occurs and when detailed error data is available. For a runtime error, the File and Line attributes are usually valid, but the column attribute often returns-1. If the error is a syntax error that prevents the page from being processed by ASP, the Source attribute is returned. Generally, the Line and Column attributes are valid in these cases. If you write the value of the Source attribute to the page, it is wise to pass the value to HTMLEncode first to prevent it from containing illegal HTML characters. The HTMLEncode method will be discussed in detail later in this chapter.

ERR Object

Tips: This is the second time I wrote this article, NND. The focus of the previous writing was the introduction of the ASPError object. Now I will introduce the err Object, which is a very simple and easy-to-operate object, let's go ., on the asp page.

When the err Object is used, you do not need to create an instance. That is to say, you can use it whenever you use it. Just like session, you do not need to Set conn = Server when using the ADODB object. createObject ("ADODB. connection ") to create an instance, it returns an error code, but Err! = Err. Number, which can be cleared by using the Clear method to facilitate next use. It mainly involves the Description method and returns a brief error Description. Here is a classic example:

<% @ LANGUAGE = "VBscript" %>
<% Response. Buffer = True
On Error Resume Next
%>
<%
S = "sa"
Response. write (Int (s ))
If Err. Number <> 0 Then
Response. Clear
Response. write "error:" %>
<HTML>
<HEAD>
<TITLE> </TITLE>
</HEAD>
<BODY>
Error Number: <% = Err. Number %> <br/>
Error message: <% = Err. Description %> <br/>
Error file: <% = Err. Source %> <br/>
Error Line: <% = Err. Line %> <br/>
<% = Err %>
</BODY>
</HTML>
<% End If %>

Run the command to check whether the queue and Err. Line are empty. Why? The line method in asp vb is not supported. This is a waste attribute supported by vb. jscript, And the catch method to be studied.

It is worth noting that when using the err Object, The On Error Resume Next must be added, and the exception that crossed the asperror object is thrown.

When connecting to the database, you can use the error object: Count attribute to Count the number of Errors sets. The Item method is used to specify a specific Error. The syntax is error. item (number), where number is a number. Because Item is the default method, the Error (number) method is equivalent to the previous method. The following is a program. Used to list Error objects:
<%
On Error Resume next
Set conn = Server. CreateObject ("ADODB. Connection ")
Dim I, your_databasepath: your_databasepath = "no. mdb"
Connstr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" & server. mappath (your_databasepath )&""
Conn. open connstr
If conn. errors. count <> 0 then
Response. write "failed to connect to database For I = 0 to conn. errors. count-1
Response. write conn. errors. item (I) & "Response. write Err. Description
Next
Else
Response. write "succeeded in connecting to the Database"
End if
Conn. close
%>

No difference with the err Object. Have you seen the comparison result? It's easy to use the err Object directly.

Generally, we recommend that you use the asperror object during debugging, that is, to set the On Error Resume next line to rem. By default, asperror is thrown. you can use the err Object To Do Something unless otherwise required. auto: http://www.jzxue.com/Html/asp/2007/9/456KJE91.html

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.