7.4.2 VBScript錯(cuò)誤處理 在VBScript中,可以使腳本解釋器不處理其找到的任何錯(cuò)誤,并且使用On Error Resume Next語句繼續(xù)運(yùn)行下個(gè)語句。一旦這個(gè)語句已被處理,腳本引擎將繼續(xù)運(yùn)行后面的程序,而不理會(huì)已經(jīng)發(fā)現(xiàn)的任何錯(cuò)誤。然而,這種過程僅適用于順序執(zhí)行語句的環(huán)境,換句話說,不適用于嵌套的函數(shù)或子程序。 1. 使用On Error Resume Next語句 一個(gè)錯(cuò)誤在子程序中出現(xiàn)時(shí),如果沒有運(yùn)行On Error Resume Next語句,那么錯(cuò)誤將被交給調(diào)用它的環(huán)境,這個(gè)過程一直重復(fù)到找到運(yùn)行On Error Resume Next語句的環(huán)境繼續(xù)運(yùn)行,或者找到缺省的腳本錯(cuò)誤處理器,把錯(cuò)誤交給ASP并且IIS顯示缺省錯(cuò)誤網(wǎng)頁(yè)。 這種錯(cuò)誤調(diào)用鏈意味著可以創(chuàng)建防止使程序停止運(yùn)行的運(yùn)行期錯(cuò)誤的函數(shù)和子程序。如果在子程序的開頭放置一個(gè)On Error Resume Next語句,任何運(yùn)行期錯(cuò)誤會(huì)中止這個(gè)子程序的運(yùn)行,但是調(diào)用該子程序的程序?qū)⒗^續(xù)運(yùn)行而不會(huì)引起網(wǎng)頁(yè)的停止。 例如,如果需要向一個(gè)文件中寫入字符串,可以通過一個(gè)獨(dú)立的函數(shù)對(duì)文件進(jìn)行訪問文件,防止錯(cuò)誤中斷整個(gè)程序的運(yùn)行: ' 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 注意上面的程序在試圖處理每個(gè)程序語句之前,先檢查VBScript的Err對(duì)象的Number屬性。如果這個(gè)值為0(還沒有出現(xiàn)錯(cuò)誤),那么就能夠繼續(xù)對(duì)文件的定入和創(chuàng)建過程。然而如果錯(cuò)誤確實(shí)發(fā)生了,腳本引擎將設(shè)置Err對(duì)象的屬性的值,并且繼續(xù)處理下一行。 只要不引起錯(cuò)誤而能正常運(yùn)行,函數(shù)的返回值將設(shè)置為“True”。否則函數(shù)將返回“False”。在編程中可以在對(duì)其進(jìn)行測(cè)試以后,再使用該函數(shù)和采取其他行動(dòng)。 下面是一個(gè)簡(jiǎn)單的例子,我們希望對(duì)任務(wù)的第一部分采用一個(gè)獨(dú)立的函數(shù),以便能更精確地辨別出錯(cuò)誤產(chǎn)生在何處。這樣,調(diào)試時(shí)也更容易閱讀代碼。在頁(yè)面的主程序中,可以調(diào)用三個(gè)單獨(dú)的函數(shù)。 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. 使用On Error Goto 0 在ASP 2.0(盡管沒有文檔記錄)和ASP 3.0中,也能使用On Error Goto 0語句恢復(fù)缺省的錯(cuò)誤處理行為。在運(yùn)行這個(gè)語句后,發(fā)生的運(yùn)行期錯(cuò)誤將導(dǎo)致缺省錯(cuò)誤處理,在環(huán)境鏈中檢查每個(gè)嵌套的程序,直到主頁(yè)面代碼。如果沒有其他的環(huán)境關(guān)閉缺省錯(cuò)誤處理,網(wǎng)頁(yè)的執(zhí)行將停止并顯示IIS缺省錯(cuò)誤網(wǎng)頁(yè)。 3. VBScript Err對(duì)象 在前面的例子中,關(guān)閉缺省錯(cuò)誤處理時(shí),通過檢查VBScript Err對(duì)象的Number屬性,查看錯(cuò)誤是否已經(jīng)出現(xiàn)。Err對(duì)象存儲(chǔ)了關(guān)于運(yùn)行期錯(cuò)誤的信息,表7-3和表7-4給出了VBScript Err對(duì)象提供的方法和屬性。 表7-3 VBScript Err對(duì)象的方法 方 法 說 明
Clear 清除當(dāng)前所有的Err對(duì)象設(shè)置
Raise 產(chǎn)生一個(gè)運(yùn)行期錯(cuò)誤
表7-4 VBScript Err對(duì)象的屬性 屬 性 說 明
Description 設(shè)置或返回一個(gè)描述錯(cuò)誤的字符串
Number (缺省)設(shè)置或返回指定一個(gè)錯(cuò)誤的值
Source 設(shè)置或返回產(chǎn)生錯(cuò)誤的對(duì)象的名稱
使用這些屬性可以檢查發(fā)生了哪種錯(cuò)誤。例如,可以根據(jù)錯(cuò)誤號(hào)采取不同的措施,也可以用Source和Description的屬性值為用戶提供錯(cuò)誤信息,或者傳送到一個(gè)文件中。 也可以使用Err對(duì)象生成一個(gè)錯(cuò)誤。為什么要做這些呢?因?yàn)橛袝r(shí)想把一個(gè)定制的錯(cuò)誤消息傳送給用戶。可以把Err對(duì)象的屬性設(shè)置成所希望的任何值。然后調(diào)用Raise方法來產(chǎn)生這種錯(cuò)誤,這樣做會(huì)停止程序的運(yùn)行,并且把錯(cuò)誤沿調(diào)用鏈向回傳遞。 下面的例子顯示了在服務(wù)器磁盤上讀取一個(gè)文本文件時(shí),如何處理錯(cuò)誤。注意如何使用常數(shù)vbObjectError,以確定所選擇的錯(cuò)誤號(hào)不會(huì)和一個(gè)已存在的錯(cuò)誤號(hào)混淆。通過把任意選擇的錯(cuò)誤號(hào)加到此常數(shù)中,就能夠保證和預(yù)定義的錯(cuò)誤不混淆。 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 strErrDescription = "The file has been deleted or moved. " strErrSource = " ReadThisFile function" Err.Raise intErrNumber, strErrSource, strErrDescription Exit Function Case Else ' som other error ' raise the standard error back up the call chain Err.Raise Err.Number, Err.Source, Err.Description Exit Function End Select ReadThisFile = objFile.ReadAll ' we opened it OK, so return the content objFile.Close End Function 調(diào)用這個(gè)函數(shù)的代碼可以使用On Error Resume Next語句,并且能捕獲這個(gè)函數(shù)產(chǎn)生的錯(cuò)誤。 On Error Resume Next strContent = ReadThisFile("myfile,txt") If Err.Number = 0 Then Response.Write "File content is:<BR>" & strContent Else Response.Write "Err.Source & "<BR>" & Err.Description End If
7.4.3 JScript錯(cuò)誤處理 在5.0版之前,JScript的錯(cuò)誤處理處理能力并不出色,然而在5.0版中情況改變了,JScript采用了一套和Java以及C++非常類似的錯(cuò)誤處理系統(tǒng)。它掌握起來盡管不像VBScript技術(shù)那樣容易,但人們認(rèn)為在錯(cuò)誤處理上,JScript走在前頭。 在第1章中,在討論這兩個(gè)主要腳本語言的新特點(diǎn)時(shí)候,已詳細(xì)討論了JScript錯(cuò)誤處理,這里不再重復(fù)。如果閱讀第1章時(shí)跳過了這部分,可以回到那里看看。
|
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!