2010-06-22 50 views

回答

3

号的VB6 manual makes it clearOn Error Goto 0仅仅影响当前程序:

On Error Goto 0禁止任何支持 错误处理程序在当前 程序。

编辑现在有一个除了问题,这是不存在的时候我张贴了这个答案。问题是“如果我在错误处理程序块中显示错误号为0的输出,那么它表示什么?”。有关答案,请参阅Mike's answer.

2

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

您的引用足够真实,但实际上并未回答问题。 – MarkJ 2010-06-22 09:23:22

+0

答案是“否”,引用只是一个解释(基本上,错误处理程序的范围是一个过程和所有调用过程不会安装它们自己的错误处理程序)。但我认为报价说得更好...... – jmoreno 2010-06-22 09:31:03

2

如果我在错误处理程序块中显示错误编号为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,因为没有发生错误。

相关问题