隆回一中电话:On Error Resume Next:VBscript错误处理

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 10:05:41

  在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
"
         If WriteContent(strContent) Then                                 '''''''' write the content
                 Response.Write "Content written to file
"
         Else
                 Response.Write "ERROR: Failed to write to the file
"
         End If
         If CloseFile(strFileName) Then
                 Response.Write "File closed
"
         Else
                 Response.Write "ERROR: Failed to close the file
"
         End If
Else
         Response.Write "ERROR: Failed to create the new file
"
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对象提供的方法和属性。

Clear 
清除当前所有的Err对象设置

Raise 
产生一个运行期错误

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:
" & strContent
Else
         Response.Write "Err.Source & "
" & Err.Description
End If