本文提供一种QTP运行错误的捕捉和记录方法,结合QTP本身的场景恢复机制,可以很好的解决该问题
  首先在恢复场景中定义场景恢复策略,选择需要捕捉的错误种类,定义捕捉错误后的处理,此处定义一个异常处理函数
  函数定义如下:
  '----------------------------------------------------------------
  '函数功能:对象错误记录
  '修改说明: ADD BY
  '----------------------------------------------------------------
  ‘ Object-出错对象;Method-对象当前的方法;Argu-对象当前方法的参数;retval-当前错误的返回值(errCode)
  Function RecordErr(Object, Method, Arguments, retVal)
  wait 3
  Dim ErrCode,ErrDesc,ArgCount,WrongInfo,ArgList
  Dim i,blankStr,ErrCount,CurRow
  ‘在Globaltable中记录累计错误数
  CurRow = DataTable.GlobalSheet.GetcurrentRow
  DataTable.GlobalSheet.SetcurrentRow 1
  If Environment ("ActionName")<> datatable.Value("CurAction",dtGlobalSheet) Then
  datatable.Value("CurAction",dtGlobalSheet) = Environment ("ActionName")
  datatable.Value("ErrCount",dtGlobalSheet) = 0
  End If
  ErrCount = CInt (datatable.Value("ErrCount",dtGlobalSheet))
  blankStr = " "
  ErrCode = retVal
  If ErrCode = 0 Then
  ErrDesc = "不明错误!"
  Else
  ErrDesc = DescribeResult(ErrCode)
  End If
  ArgCount = UBound(Arguments)
  For i = 0 to ArgCount
  ArgList = ArgList & CStr(i) & "--" & CStr(Arguments(i)) & "/"
  Next
  On Error Resume Next
  ‘获取对象错误具体描述,此处使用了对象较通用的name属性,部分对象没有该属性可能出错
  WrongInfo = "Object Name: " & Object.GetTOProperty("name") & vbCrLf _
  & blankStr & "Current Method: " & Method & vbCrLf _
  & blankStr & "ArgList: " & ArgList & vbCrLf _
  & blankStr & "ErrDesc: " & ErrDesc
  On Error GoTo 0
  GE_logError "对象运行中出现错误--" & WrongInfo,micFail
  Err.Clear
  ErrCount = ErrCount + 1
  ‘累计错误达5次后退出当前流程
  If ErrCount >= 5 Then
  ErrCount = 0
  datatable.Value("ErrCount",dtGlobalSheet) = ErrCount
  DataTable.GlobalSheet.SetcurrentRow CurRow
  routingname = ""
  GE_logError "*****运行过程错误过多,退出本Aciton Iteration,请运行完成后检查环境和脚本!*****",micFail
  ExitActionIteration
  End If
  datatable.Value("ErrCount",dtGlobalSheet) = ErrCount
  DataTable.GlobalSheet.SetcurrentRow CurRow