VB On Error 使用详解

VB On Error 使用详解VBOnError 使用详解

打不开MSDN,转载网友一篇,原帖地址VB On Error 使用详解

1. 微软提供的On Error Goto 的使用

To prevent error-handling code from running when no error has occurred, place an Exit SubExit Function, or Exit Property statement immediately before the error-handling routine, as in the following fragment:

Public Sub InitializeMatrix(ByVal Var1 As ObjectByVal Var2 As Object)
   On Error GoTo ErrorHandler
   ‘ Insert code that might generate an error here
   Exit Sub
ErrorHandler:
   ‘ Insert code to handle the error here
   Resume Next
End Sub

Here, the error-handling code follows the Exit Sub statement and precedes the End Sub statement to separate it from the procedure flow.








 

2 运行时错误要用On Error Resume Next,然后If Err.Number=…

The On Error Resume Next construct may be preferable to On Error GoTowhen handling errors generated during access to other objects. Checking Err after each interaction with an object removes ambiguity about which object was accessed by the code. You can be sure which object placed the error code in Err.Number, as well as which object originally generated the error (the object specified in Err.Source).

 

3.Example



This example first uses the On Error GoTo statement to specify the location of an error-handling routine within a procedure. In the example, an attempt to divide by zero generates error number 6. The error is handled in the error-handling routine, and control is then returned to the statement that caused the error. The On Error GoTo 0 statement turns off error trapping. Then the On Error Resume Next statement is used to defer error trapping so that the context for the error generated by the next statement can be known for certain. Note that Err.Clear is used to clear the Errobject’s properties after the error is handled.

Public Sub OnErrorDemo()
   On Error GoTo ErrorHandler   ‘ Enable error-handling routine.
   Dim x As Integer = 32
   Dim y As Integer = 0
   Dim z As Integer
   z = x / y   ‘ Creates a divide by zero error
   On Error GoTo 0   ‘ Turn off error trapping.
   On Error Resume Next   ‘ Defer error trapping.
   z = x / y   ‘ Creates a divide by zero error again
   If Err.Number = 6 Then
      ‘ Tell user what happened. Then clear the Err object.
      Dim Msg As String
      Msg = “There was an error attempting to divide by zero!”
      MsgBox(Msg, , “Divide by zero error”)
      Err.Clear() ‘ Clear Err object fields.
   End If
Exit Sub      ‘ Exit to avoid handler.
ErrorHandler:  ‘ Error-handling routine.
   Select Case Err.Number   ‘ Evaluate error number.
      Case 6   ‘ Divide by zero error
         MsgBox(“You attempted to divide by zero!”)
         ‘ Insert code to handle this error
      Case Else
         ‘ Insert code to handle other situations here…
   End Select
   Resume Next  ‘ Resume execution at same line
                ‘ that caused the error.
End Sub


























版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/226654.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月16日 下午10:26
下一篇 2026年3月16日 下午10:26


相关推荐

  • 关于C语言中return的总结

    关于C语言中return的总结return break 和 continue 这三个关键字有一个共同点 那就是读能让后面的语句不执行 不同的地方就是挑的距离不一样 return 很强大 如果一个函数中有一个 return 并且执行了 那么这个函数就完了 return 表示从被调函数返回到主调函数继续执行 返回时可附带一个返回值 由 return 后面的参数指定 return 通常是必要的 因为函数调用的时候计算结果通常是通过返回值带出的 如

    2026年3月20日
    2
  • cocoapods最新版本_cocoapods使用

    cocoapods最新版本_cocoapods使用CocoaPods简介CocoaPods负责管理iOS项目中第三方框架。CocoaPods的项目源码在Github上管理。项目从2011年8月12日开始,CocoaPods的出现使得我们可以节省设置和更新第三方开源库的时间。(练习时为了速度一般我都是直接导入工程中,个人比较讨厌写纯代码在Podfile文件中)开始安装安装需要用到Ruby,虽然Mac自带了Ruby,不过版本有点老了,最好更新一…

    2025年5月23日
    2
  • 什么软件可以更换手机ip地址「建议收藏」

    什么软件可以更换手机ip地址「建议收藏」这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML图表FLowchart流程图导出与导入导出导入欢迎使用Markdown编辑器你好!这是你第一次使用Markdown编辑器所展示的欢迎页。如果你想学习如何使用Mar

    2022年6月17日
    31
  • Doris Compaction机制总结

    Doris Compaction机制总结1 参考文档 Doris 最佳实践 Compaction 调优 1 Doris 最佳实践 Compaction 调优 2 Doris 全面解析 DorisCompact 机制解析按顺序读完这三篇文章 就能对 Doris 的 compaction 机制很熟悉了 2 总结 2 1 读写方式 2 1 1 写入 Doris 数据写入模型使用了 LSM Tree 随机写变为顺序写 面向写优化 数据追加的方式写入磁盘 2 1 2 读取读逻辑上 需要通过 Merge on Read 方式 2 2 3 compaction 目的

    2025年7月9日
    4
  • webstorm激活码【注册码】

    webstorm激活码【注册码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    45
  • Oracle函数——TO_DATE[通俗易懂]

    Oracle函数——TO_DATE[通俗易懂]TO_DATE含义:将具有固定格式的字符串类型的数据转化为相对应的Date类型数据,官网解释如下图使用方法TO_DATE("需要转换的字符串","日期格式&quot

    2022年7月3日
    41

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号