在VBScript中,有一個On Error Resume Next語句,它使腳本解釋器忽略運行期錯誤并繼續(xù)腳本代碼的執(zhí)行。接著該腳本可以檢查Err.Number屬性的值,判別是否出現了錯誤。如果出現錯誤,返回一個非零值。在ASP3.0中,也可以使用OnErrorGoto0“轉回到”缺省的錯誤處理。在ASP2.0中實際也進行這種處理,但是沒有相應文檔說明,這在很多asp數據相關處理文件中司空見慣,加上On Error Resume Next,關閉缺省的錯誤處理,然后用err抓住,
If Err Thenerr.ClearResponse.Write "出現了錯誤!"Response.EndEnd If
為了得到更加詳細的錯誤說明,我們就試試asperror對象吧,它是asp3.0的新對象,它可以通過server對象的getlasterror方法得到,asperror提供了關于asp中發(fā)生最后一個錯誤的詳細信息,與VBScript的Err對象不同,不能為查看是否出現了錯誤而隨時調用該方法,只能在一個ASP定制的錯誤網頁中使用。如果像對Err對象進行操作那樣,通過關閉缺省的錯誤處理(用On Error Resume Next語句)來使用,則GetLastError方法不能訪問錯誤的詳細數據。
ASPError對象的屬性:
ASPError對象提供了九個屬性說明所出現的錯誤的性質和錯誤源,并返回引發(fā)錯誤的實際代碼,其屬性及說明如下:
ASPCode:整型。由ASP/IIS產生的錯誤號,例如0x800A009
ASPDescription: 字符串型。如果這個錯誤是與ASP相關的錯誤,這個屬性是錯誤的詳細說明.例如:AllHTTP:HTTP_ACCEPT:*/*HTTP_ACCEPT_LANGUAGE:zh-cnHTTP_CONNECTION:Keep-AliveHTTP_HOST:sHTTP_USER_AGENT:Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.0;(R11.5))...還有cookie等報告.
Category:字符串型。錯誤來源,即ASP內部腳本語言、或一個對象.
Column:整型。產生錯誤的文件中的字符位置
Description:字符串型。錯誤的簡短說明
File:字符串型。錯誤出現時正在處理的文件的名稱
Line:整型。產生錯誤的文件中的行號
Number:整型。一個標準的COM錯誤代碼
Source:字符串型。引發(fā)錯誤的行的實際代碼
ok,這就是9個屬性,使用asperror對象的語法是:
asperror.property
就是這樣:
ASPError.ASPCode()
ASPError.ASPDescription()
ASPError.Category()
ASPError.Column()
ASPError.Description()
ASPError.File()
ASPError.Line()
ASPError.Number()
ASPError.Source()
在iis支持的所有目錄下面(或:在編輯了錯誤映射屬性的目錄內)的任一頁面上出現一個與ASP相關的錯誤時,都將載入定制錯誤頁面。實際上,現在已經設置了一個正常的腳本錯誤陷阱,因為在這個目錄內的任何一個網頁上的ASP運行期錯誤都將觸發(fā)定制錯誤頁面,錯誤網頁作為IIS的缺省安裝部分,可根據個人情況定制.例如,當我們在一個目錄下面輸入不存在的網頁時,出現404錯誤,當一個404錯誤出現時,使用的頁面是404b.htm,這個文件包含一個客戶端腳本代碼部分,它獲得當前文檔的URL(從document對象的url屬性中檢索)并在該頁面中顯示:
<!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>無法找到網頁</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, '&').replace(/'/g, '"').replace(/</g, '<').replace(/>/g, '>');}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-->無法找到網頁</h1> </td> </tr> <tr><td width="400" colspan="2"> <font style="COLOR:000000; FONT: 9pt/11pt 宋體">您正在搜索的網頁可能已經刪除、更名或暫時不可用。</font></td> </tr> <tr> <td width="400" colspan="2"> <font style="COLOR:000000; FONT: 9pt/11pt 宋體"> <hr color="#C0C0C0" noshade><p>請嘗試下列操作:</p> <ul><li>如果您在“地址”欄中鍵入了網頁地址,請檢查其拼寫是否正確。<br> </li><li>打開 <script> <!-- if (!((window.navigator.userAgent.indexOf("MSIE") > 0) && (window.navigator.appVersion.charAt(0) == "2"))) { Homepage(); } //--> </script> 主頁,尋找指向所需信息的鏈接。</li><li>單擊<a href="javascript:history.back(1)">后退</a>按鈕嘗試其他鏈接。</li> </ul><h2 style="font:9pt/11pt 宋體; color:000000">HTTP 404 - 無法找到文件<br> Internet 信息服務<BR></h2> <hr color="#C0C0C0" noshade> <p>技術信息(支持個人)</p><ul><li>詳細信息:<br><a target="_blank">Microsoft 支持</a></li></ul> </font></td> </tr></table></body></html>以上就是對ASP error對象的全部簡析,希望對大家的學習有所幫助。
新聞熱點
疑難解答