【VB】On Error略解


On Error GoTo 0

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 Err object’s properties after the error is handled.


On Error Resume Next

The On Error Resume Next construct may be preferable to On Error GoTo when 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).

访问对象时要使用这种形式而不使用 On Error GoTo。

On Error GoTo line

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.

启动错误处理程序,且该例程从必要的 line 参数中指定的line开始。line 参数可以是任何行标签或行号。
如果发生一个运行时错误,则控件会跳到 line,激活错误处理程序。
指定的 line 必须在一个过程中,这个过程与 On Error 语句相同; 否则会发生编译时间错误。
(和Exit Sub连用)

一般情况下,如果在对我们创建的对象或控件进行错误捕捉,需要使用On Error Resume Next ,然后在判断它的Err.Number,根据错误类型来做相应的处理。


VB On Error 使用详解@阿英