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