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

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

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

Public Sub InitializeMatrix(ByVal Var1 As Object, ByVal 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

VB On Error 使用详解相关推荐

  1. vb调用excel方法详解及操作相关操作命令大全

    如果你要在VB中要想调用Excel,需要打开VB编程环境"工程"菜单中的"引用"项目,并选取项目中的"Microsoft Excel 11.0 obj ...

  2. golang error类型详解

    error类型是go语言的一种内置类型,使用的时候不用特定去import,他本质上是一个接口, type error interface{Error() string //Error()是每一个订制的 ...

  3. VB 子类化技术详解

    1. 何谓子类化(subclassing) 众所周知,Windows是一个基于消息的系统,消息在Windows的对象之间进行着传递.子类化和Windows的钩子机制存在于消息系统之中,我们可以利用这些 ...

  4. html用vb脚本写累加,详解HTML和VBScript注释

    JSP 语法 例子 1 在客户端的HTML源代码中产生和上面一样的数据: 例子 2 在客户端的HTML源代码中显示为: 描述 这种注释和HTML中很像,也就是它可以在"查看源代码" ...

  5. Log4j写入数据库详解

    log4j是一个优秀的开源日志记录项目,我们不仅可以对输出的日志的格式自定义,还可以自己定义日志输出的目的地,比如:屏幕,文本文件,数据库,甚至能通过socket输出.本节主要讲述如何将日志信息输入到 ...

  6. 从零开始玩转 logback、完整配置详解

    官网地址:https://logback.qos.ch/manual/index.html 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 概述 L ...

  7. 【转】用VB操作Excel详解

    用VB操作Excel详解 一. VB读写EXCEL表: VB本身提自动化功能可以读写EXCEL表,其方法如下: 1.在工程中引用MicrosoftExcel 类型库: 从"工程"菜 ...

  8. 回归分析评估指标均方对数误差(MSLE)详解及其意义:Mean Squared Log Error

    回归分析评估指标均方对数误差(MSLE)详解及其意义:Mean Squared Log Error 目录 回归分析评估指标均方对数误差(MSLE)详解及其意义:Mean Squared Log Err ...

  9. [转]VB中资源文件.res的使用方法详解

    在几乎所有的Windows应用程序中都拥有资源文件,这些文件定义使用应用程序将要显示很多的用户界面元素,以及提供程序所需要的各种类型数据的存储场所.资源文件在VC应用程序中应用十分广泛,在VB应用中却 ...

最新文章

  1. iic通信原理_电子知识之IIC通信原理和协议分享
  2. OWC的问题——散点图与折线图如何才能同时显示
  3. CocosEditor 1.0Final-IDEA13.0和1.5-IDEA13.1 发布–2014.03.25
  4. Docker入门系列之三:如何将dockerfile制作好的镜像发布到Docker hub上
  5. 系统测试集成测试单元测试_单元和集成测试的代码覆盖率
  6. 一位前辈工程师职业发展的忠告
  7. (转载)关于ASCII和GB2312、GBK、GB18030、UNICODE
  8. asp.net oracle连接数据库,ASP.NET连接Oracle数据库的步骤详解
  9. oracle去除表中字段中特殊符号('tab','空格','换行符','回车')——目前没找到可以一次性替换表中所有异常数据的方法,只能对字段逐一去除,如果有还希望留言赐教
  10. 面试题10.3-变态跳台阶
  11. Bailian1193 内存分配【数据结构】
  12. 关于调试,很大的感触,请看下面的c程序
  13. 人脸识别 java_基于Java实现人脸识别功能(附源码)
  14. java实现计算器_初学JAVA之实现计算器
  15. while在Java用法_Java中while循环用法
  16. mysql 5.7 临时表_MySQL 5.7内部临时表使用
  17. 阴阳师服务器维护内容,阴阳师8月1日维护内容介绍_友人帐弈鬼切内容介绍_3DM手游...
  18. 基于评论的跨境电商产品满意度分析_kaic
  19. 秒杀品牌数据线的开博尔USB3.1Gen2 Type-C数据线评测
  20. 鸡兔同笼python程序怎么写_【鸡】鸡的功效_鸡图片_食材百科_美食杰

热门文章

  1. 个人和企事业单位都免费的office软件真的有吗?
  2. “并发冲突” 分析与解决
  3. Android开发—华为手机应用内升级之后App图标消失
  4. react+dva+antd的骚操作
  5. python pause_Python相当于系统(‘PAUSE’)
  6. 各个排序算法及其时间复杂度
  7. 星界边境的服务器信息,怎么用vps开星界边境服务器
  8. Fedora21+ Wine + QQ 6.9 (QQ2015)
  9. 如何建立起自己的3Dmax建模思维?
  10. %2d在c语言中什么作用,C语言中的 %2d是什么意思