如果在VB6 Sub中调用On Error Goto 0
,这将关闭错误处理,即使控制返回到调用Function/Sub?VB6:“On Error Goto 0”会影响调用函数的错误处理程序吗?
编辑:如果我的错误处理程序块中显示错误编号为0的输出,那么它表示什么?
如果在VB6 Sub中调用On Error Goto 0
,这将关闭错误处理,即使控制返回到调用Function/Sub?VB6:“On Error Goto 0”会影响调用函数的错误处理程序吗?
编辑:如果我的错误处理程序块中显示错误编号为0的输出,那么它表示什么?
号的VB6 manual makes it clear是On Error Goto 0
仅仅影响当前程序:
On Error Goto 0
禁止任何支持 错误处理程序在当前 程序。
编辑现在有一个除了问题,这是不存在的时候我张贴了这个答案。问题是“如果我在错误处理程序块中显示错误号为0的输出,那么它表示什么?”。有关答案,请参阅Mike's answer.
号
http://www.vb-helper.com/tut6.htm
When a program encounters an error, Visual Basic checks to see if an error handler is presently installed in the current routine. If so, control passes to that error handler. If no error handler is in effect, Visual Basic moves up the call stack to the calling routine to see if an error handler is currently installed there. If so, the system resumes execution at that error handler. If no error handler is installed in the calling routine either, Visual Basic continues moving up the call stack until it finds a routine with an error handler installed. If it runs off the top of the stack before it finds an active error handler, the program crashes.
如果我在错误处理程序块中显示错误编号为0的输出,那么它表示什么?
这意味着Err
对象没有在您签了Err.Number
属性的代码点包含错误信息。这种情况可能是由一些不同的原因:
Err
对象之前调用明确清零Err.Clear
Err
对象是通过调用On Error Goto
清除。 On Error Goto
声明将清除当前对象Err
对象Err
对象被Resume X
语句清除。与普通的Goto X
声明,一个Resume
将清除当前Err
对象(并提高其自身的错误,如果Err
对象已经是空的)到达误差之前电流Sub
/Function
/Property
你忘了退出处理程序,例如:
Public Sub SampleRoutine
On Error Goto ErrorHandler
DoSomething
DoSomethingElse
' You need an Exit Sub here so that the code does not reach the error handler'
'in the event no error occurs.'
ErrorHandler:
MsgBox Err.Number & "-" & Err.Description
End Sub
这是我的经验,一个很常见的错误。如果您在到达错误处理程序标签之前没有明确地退出例程,那么即使没有错误发生,错误处理程序中的代码仍会运行。在这种情况下,Err.Number
将为0,因为没有发生错误。
您的引用足够真实,但实际上并未回答问题。 – MarkJ 2010-06-22 09:23:22
答案是“否”,引用只是一个解释(基本上,错误处理程序的范围是一个过程和所有调用过程不会安装它们自己的错误处理程序)。但我认为报价说得更好...... – jmoreno 2010-06-22 09:31:03