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