打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【error对象2】错误号(即Err.Number)的详解


'程序的错误类型
'编译错误(Compile Error)是在编译应用程序过程中发现的错误。这类错误一般是由于不正确的代码结构而产生
'运行错误(RunTime Error)指应用程序在运行时发生的错误,例如,程序代码执行了非法操作或某些操作失败。发生这类错误时,
  '                                                        程序的语法一般没有错误,并且编译时也能通过,只在运行时发现。如被零除,溢出错误(变量赋值不当)
'逻辑错误(Logic Error)指应用程序的运行结果与预期结果不相同。此时,程序中没有语法错误和编译错误,程序也可以运行,但运行结果不对。
                                                        这种错误通常是由于程序中存在逻辑上的缺陷而引起。

DividedByZero

' 遇到错误跳出
Sub DividedByZero
  Dim a, b,c
  a =10 : b=0
      c = cDbl(a/b)
   b =3
    c = cDbl(a/b)
   Print c

  End Sub

 

'On Error Resume Next
' 遇到错误继续,把系统错误记录下来
  Sub DividedByZero1
  On error resume next
  Dim a, b,c
  a =10 : b=0
      c = cDbl(a/b)
   Print cStr(err.number) & " " & err.description

  b =3
    c = cDbl(a/b)
   Print c

  End Sub


' 遇到错误继续,抛出自己定义的错误
DividedByZero2

'Error number1: 11 error description: 被零除 at source of:  Microsoft VBScript 运行时错误
'Error number2: 1000 error description: Divided by zero error defined by Jerry  at source of:  My Error Source
'3.33333333333333

 

 Sub DividedByZero2
  On error resume next
  Dim a, b,c
  a =10 : b=0
      c = cDbl(a/b)
    Print  "Error number1: " & cStr(err.number) & " error description: " & err.description & " at source of:  "& err.source

 Err.Raise  err.Number, error.source, error.description
   Err.Raise  1000, "My Error Source", "Divided by zero error defined by Jerry " '自定义,错误
   Print  "Error number2: " & cStr(err.number) & " error description: " & err.description & " at source of:  "& err.source
   Err.Clear   '清除错误。

  b =3
    c = cDbl(a/b)
   Print c

  End Sub

错误号(即Err.Number)的详解
  Number:1
 Description:应用程序定义的错误或对象定义的错误。 
  Number:2 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:3 
 Description:此错误号已过时,不再使用。 
  Number:4 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:5 
 Description:过程调用或参数无效。  
  Number:6 
  Description:溢出。 
  Number:7 
  Description:内存不足。 
  Number:8 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:9 
  Description:下标超出范围。 
  Number:10 
  Description:此数组被固定或临时锁定。 
  Number:11 
  Description:被零除。 
  Number:12 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:13 
  Description:类型不匹配。 
  Number:14 
  Description:字符串空间不足。 
  Number:15 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:16 
  Description:表达式过于复杂。 
  Number:17 
  Description:无法执行请求的操作。 
  Number:18 
  Description:发生了用户中断。 
  Number:19 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:20 
  Description:无错误继续执行。 
  Number:21 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:22 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:23 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:24 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:25 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:26 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:27 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:28 
  Description:堆栈空间不足。 
  Number:29 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:30 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:31 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:32 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:33 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:34 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:35 
 Description:未定义  Sub  或  Function。 
  Number:36 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:37 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:38 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:39 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:40 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:41 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:42 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:43 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:44 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:45 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:46 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:47 
 Description:DLL  应用程序客户端太多。 
  Number:48 
 Description:加载  DLL  时出错。 
  Number:49 
 Description:错误的  DLL  调用约定。 
  Number:50 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:51 
  Description:内部错误。 
  Number:52 
  Description:文件名或编号错误。 
  Number:53 
  Description:未找到文件。 
  Number:54 
  Description:错误的文件模式。 
  Number:55 
  Description:文件已打开。 
  Number:56 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:57 
 Description:设备  I/O   错误。 
  Number:58 
  Description:文件已存在。 
  Number:59 
  Description:错误的记录长度。 
  Number:60 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:61 
  Description:磁盘已满。 
  Number:62 
  Description:输入超出文件尾。 
  Number:63 
  Description:错误的记录号。 
  Number:64 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:65 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:66 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:67 
  Description:文件太多。 
  Number:68 
  Description:设备不可用。 
  Number:69 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:70 
  Description:权限被拒绝。 
  Number:71 
  Description:磁盘未准备好。 
  Number:72 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:73 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:74 
 Description:无法用不同的驱动器重命名。 
  Number:75 
  Description:路径/文件访问错误。 
  Number:76 
  Description:未找到路径。 
  Number:77 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:78 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:79 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:80 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:81 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:82 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:83 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:84 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:85 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:86 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:87 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:88 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:89 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:90 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:91 
 Description:未设置对象变量或  With  块变量。 
  Number:92 
 Description:未初始化  For   循环。 
  Number:93 
  Description:模式串无效。 
  Number:94 
 Description:此错误号已过时,不再使用。 
  Number:95 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:96 
 Description:无法接收对象的事件,原因是该对象激发的事件数已达到它所支持的事件接收器的最大数。 
  Number:97 
 Description:在不是定义类的实例的对象上不能调用友元函数。 
  Number:98 
 Description:无论是作为参数还是作为返回值,属性或方法调用都不能包括对私有对象的引用。 
  Number:99 
 Description:应用程序定义的错误或对象定义的错误。 
  Number:100 
 Description:类“|1”不实现  System.Collections.ICollection  接口。 
  Number:101 
 Description:应用程序定义的错误或对象定义的错误。  


On Error Resume Next 会使程序从紧随产生错误的语句之后的语句继续执行,或是从紧随最近一次调用含有 On Error Resume Next 语句的过程的语句继续运行。这个语句可以置运行时错误于不顾,使程序得以继续执行。可以将错误处理程序放置在错误发生的地方,而不必将控件传输到过程中的其它位置。在调用另一个过程时,On Error Resume Next 语句成为非活动的,所以,如果希望在例程中进行嵌入错误处理,则应在每一个调用的例程中执行 On Error Resume Next 语句。


On Error Goto 0  '使用这条语句会让系统不再沉默,一旦有错误发生就会提示错误,并结束脚本执行。
'An On Error Resume Next statement becomes inactive when another procedure is called, so you should execute an On Error Resume Next statement in each called routine if you want inline error handling within that routine. When a procedure is exited, the error-handling capability reverts to whatever error-handling was in place before entering the exited procedure.
'
'Use On Error GoTo 0 to disable error handling if you have previously enabled it using On Error Resume Next. ' 明明enable error 为什么是disable,不理解。

 

'Run error after " On Error Goto 0"
'Divided by zero error defined by Fisher

'Line (11): "Err.Raise  1000, "My Error Source", "Divided by zero error defined by Jerry " '自定义,错误".

 

Dim arr(1) ' 数组必须在Sub DividedByZero4 外定义,才能被Ubound(arr)识别

DividedByZero4

For i = 0  to Ubound(arr)
errType(arr(i))

Next

 

Sub DividedByZero4
  On Error resume next
  Dim a, b,c
  a =10 : b=0
      c = cDbl(a/b)
         arr(0) =  cStr(err.number)

 On Error Goto 0  '使用这条语句会让系统不再沉默,一旦有错误发生就会提示错误,并结束脚本执行。
 Err.Raise  err.Number, error.source, error.description
   Err.Raise  1000, "My Error Source", "Divided by zero error defined by Fisher" '自定义错误
   ' runtime error, click Ok to run following statement. or stop here.

   Print  "Error number: " & cStr(err.number) & " error description: " & err.description & " at source of:  "& err.source
   arr(1) =  cStr(err.number)
   Err.Clear   '清除错误。

end Sub

 

Sub errType(errNumber)
  Select Case errNumber
  Case 1000
   msgbox "This error was defined by myself."
   Case 11
    msgbox "it is the error thrown by System"
    Case else
     msgbox "no error catched!"
  End Select
 
  End Sub


'******************************************************************
'On Error GoTo行号或行标号
'qtp throws out "Syntax error": This is not supported by VBScript and so not in QTP.
'You can use 'On Error Resume Next' or 'On Error Goto 0' only in qtp scripts.
'--------------------------------------------------------------------------------

 Sub DividedByZero3
  On Error Goto errType
  Dim a, b,c
  a =10 : b=0
      c = cDbl(a/b)

 Err.Raise  err.Number, error.source, error.description
   Err.Raise  1000, "My Error Source", "Divided by zero error defined by Jerry " '自定义,错误
   Print  "Error number: " & cStr(err.number) & " error description: " & err.description & " at source of:  "& err.source
   Err.Clear   '清除错误。

   Exit sub

errType:
  Select Case err.number
  Case 1000
   msgbox "This error was defined by myself."
   Case 11
    msgbox "it is the error thrown by System"
    Case else
     msgbox "no error catched!"
  End Select
 
  End Sub

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
跟烟花入门VBA之99集:VBA错误处理(三)
VBS基础篇
On Error Resume Next:VBscript错误处理
【VBA】on error
VBA 程序之错误处理
VBA自学收集代码
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服