'on error goto 0'和'on error goto -1' - VBA的差值

时间:2022-11-19 23:54:50

Can anyone find the difference between 'On error goto -1' and 'on error goto 0' in VBA? I've tried google and msdn, but I've had no luck.

VBA中的On error goto -1和On error goto 0之间有什么区别吗?我试过谷歌和msdn,但是我运气不好。

4 个解决方案

#1


46  

On Error GoTo 0 disables any error trapping currently present in the procedure.

当发生错误时,GoTo 0将禁用当前过程中出现的任何错误捕获。

On Error GoTo -1 clears the error handling and sets it to nothing which allows you to create another error trap.

在错误后,清除错误处理并将其设置为零,这将允许您创建另一个错误陷阱。

Example: On Error GoTo -1

示例:错误上的GoTo -1

After the first error is raised, it will GoTo ErrorFound which will then clear the routine's error handling and set a new one, which will GoTo AnotherErrorFound when an error is found.

在第一个错误被提出之后,它将转到ErrorFound,然后清除例程的错误处理并设置一个新的错误,当发现错误时,它将转到另一个错误。

Sub OnErrorGotoMinusOneTest()

    On Error GoTo ErrorFound

    Err.Raise Number:=9999, Description:="Forced Error"

    Exit Sub

ErrorFound:

    On Error GoTo -1 'Clear the current error handling
    On Error GoTo AnotherErrorFound 'Set a new one
    Err.Raise Number:=10000, Description:="Another Forced Error"

AnotherErrorFound:

    'Code here

End Sub

Example: On Error GoTo 0

示例:出错后转到0

After the first error is raised, you will receive the error as error handling has been disabled.

在第一个错误被提出后,您将收到错误,因为错误处理已被禁用。

Sub OnErrorGotoZeroTest()

    On Error GoTo 0

    Err.Raise Number:=9999, Description:="Forced Error"

End Sub

#2


8  

This answer addresses the confusion between the error object and the error handler.

这个答案解决了错误对象和错误处理程序之间的混淆。

The error object can be cleared using Err.Clear. This does not affect the error handler.

可以使用error . clear清除错误对象。这不会影响错误处理程序。

The error handler becomes enabled by using On Error Goto <label>. It becomes active when an error occurs.

错误处理程序通过使用错误Goto

While the error handler is active, you can not assign a new error handler. On Error Goto <label> will have no effect. VBA simply ignores the attempt to assign a new error handler.

当错误处理程序处于活动状态时,您不能分配新的错误处理程序。在错误时,Goto

Using Err.Clear does not cancel the error handler.

用错。Clear没有取消错误处理程序。

Jumping to a different place in the code using Goto <label> does not cancel the error handler. Using Goto <label> in an error handling block can cause confusion and should be avoided. You might think the error handler is no longer active when in fact it is still active.

使用Goto

The effect of an active error handler is that you can not assign a new error handler. On Error Goto <label> will have no effect. VBA simply ignores the attempt to assign a new error handler. Any additional errors will be unhandled while the error handler is active.

主动错误处理程序的作用是不能分配新的错误处理程序。在错误时,Goto

The only way to exit an active error handler is:

退出活动错误处理程序的唯一方法是:

  1. Resume
  2. 的简历
  3. Resume Next
  4. 恢复下
  5. Resume <label>
  6. 简历 <标记>
  7. On error goto -1
  8. On error goto 1
  9. exit the procedure
  10. 退出程序

Using any one of these ways to exit the error handler will also clear the error object.

使用其中任何一种方法退出错误处理程序也将清除错误对象。

Excellent source: Pearson Error Handling In VBA Chip Pearson doesn't mention On error goto -1 in his article. To quote him:

极好的来源:皮尔森错误处理在VBA芯片皮尔逊没有提到错误goto -1在他的文章。引用他的话:

I deliberately did not include On Error GoTo -1 because it serves no real purpose and can lock up the entire Excel application unless used in exactly the right way. Yes, On Error GoTo -1 is syntactically valid, but it is like giving a gun to drunk teenager. Nothing good will come from it.

我故意不包含On Error GoTo -1,因为它没有真正的用途,并且可以锁定整个Excel应用程序,除非以正确的方式使用。是的,在错误上,GoTo -1在句法上是有效的,但这就像给醉酒的青少年一把枪。它不会带来任何好处。

You can also handle errors inline without using an error handler using the error object: MSDN Inline Error Handling

您还可以使用error对象(MSDN内联错误处理)处理内联错误,而无需使用错误处理程序

#3


2  

Here's another one

这是另一个

http://www.excelfox.com/forum/f23/error-goto-1-a-894/

http://www.excelfox.com/forum/f23/error-goto-1-a-894/

Shows a bit more on what's the difference between GoTo 0 and GoTo -1

再看一下GoTo 0和GoTo -1之间的区别。

#4


-1  

It is important to realise there are two distinct things that happen when an error occurs in VBA.

重要的是要认识到,当VBA中发生错误时,会发生两件截然不同的事情。

  1. The error object has it's properties set (ie err.number, err.desciption, err.source etc)

    错误对象具有其属性集(即err)。号码、err.desciption犯错。源等)

  2. The next line to be executed changes.
    Which line is executed is determined by the last "On Error Goto" statement that was executed - if any.

    要执行的下一行更改。执行哪一行取决于最后执行的“On Error Goto”语句——如果有的话。

These are separate but highly related topics and you will write what is in effect distinct but interwoven code to manage them both.

这些是独立的但高度相关的主题,您将编写实际不同但相互交织的代码来管理它们。

When ANY error occurs or you use Err.Raise the Err object is ALWAYS set up. Even if "On Error Resmue next" or any other On error statement has been used.

当出现错误或使用Err时。始终设置Err对象。即使使用了“On Error Resmue next”或任何其他On Error语句。

So code like this could ALWAYS be used:

所以像这样的代码总是可以使用的:

Dim i as integer 
On error resume next 
i = 100/0  ' raises error
if err.number <> 0 then 
   ' respond to the error
end if

It is really important to realise that when the error object has a non zero value for err.number an exception has been raised AND that if you then try and execute any "On Error Goto " statement doing so will raise an error and execution will be passed to any code that called the current procedure. (or where not called by any code the usual VBA error dialogue is given). Note that in this scenario "On Error Goto ALabel1" would NOT change the next line to be the line with Label1: on it.

当错误对象的值为err时,必须意识到这一点。出现了一个异常,如果您尝试执行任何“On Error Goto”语句,那么将会引发错误,执行将传递给调用当前过程的任何代码。(或者在没有被任何代码调用的地方,给出通常的VBA错误对话)。注意,在这个场景中,“On Error Goto ALabel1”不会将下一行更改为带有标签1的行:在上面。

eg

Sub ErrorTest()

    Dim dblValue        As Double

    On Error GoTo ErrHandler1
    dblValue = 1 / 0

ErrHandler1:
    debug.print "Exception Caught"
    debug.print Err.Number

    On Error GoTo ALabel1
    dblValue = 1 / 0

Exit sub
ALabel1:
    debug.print "Again caught it."

End Sub

Once the err.number property is set to non zero, you can reset it to zero by using

一旦犯错。number属性设置为non - zero,可以通过使用将其重置为zero

On Error Goto -1 

Note that Err.Clear also resets it to zero but it is actually equivalent to:

注意,犯错。Clear也将其重置为0,但它实际上等价于:

On Error Goto -1 
On Error Goto 0

ie Err.Clear removes an "On Error Goto" that is currently in place. So therefore it is mostly best to use:

ie犯错。Clear删除当前正在执行的“On Error Goto”。因此,最好用:

On Error Goto -1   

as using Err.clear You would often need to write

用错。显然你经常需要写作

Err.Clear
On Error Goto MyErrorHandlerLabel

It is worth noting that Err.Clear is implicitly carried out by VBA whenever it executes any type of Resume statement, Exit Sub, Exit Function, Exit Property, or any On Error statement.

这是值得注意的。每当执行任何类型的Resume语句、Exit Sub、Exit函数、Exit属性或任何On Error语句时,VBA都会隐式执行Clear。

You can also set the error object it to whatever number you like using

您还可以将error对象设置为您喜欢的任何数字

Err.Raise Number:=, Source:=, Description:=

犯错。提高数字:来源:=、描述:= =

Err.Raise is very important as it allows you to propagate an error to the calling program AND raise your own error numbers known as "user defined errors" that provide a means of telling the calling program that it could not continue for a logical reason. (eg a business rule was broken).

犯错。Raise非常重要,因为它允许您将错误传播到调用程序,并生成您自己的错误号,称为“用户定义的错误”,该错误号提供了一种方法,可以告诉调用程序由于逻辑原因无法继续。(例商业规则被打破)。

You can control which line of code is executed next using statements like

您可以使用下面的语句来控制执行哪一行代码

On Error Goto ALabelName On Error Goto ANonZeroLineNumber and On Error Goto 0 ' This is a special case as it in effect says "within the current scope (typically a sub or function), in the event that an error happens pass the error object back to the code that called the current sub or function.

On Error Goto ALabelName On Error Goto ANonZeroLineNumber On Error Goto 0这是一个特殊的例子,因为它实际上说:“在当前的范围内(通常是一个接头或函数),如果一个错误发生的错误对象传递回调用当前子或函数的代码。

Error handling in VBA is tricky, especially as the MSDN pages do not really give complete examples of how error handling can be used.

VBA中的错误处理很棘手,特别是MSDN页面并没有给出如何使用错误处理的完整示例。

#1


46  

On Error GoTo 0 disables any error trapping currently present in the procedure.

当发生错误时,GoTo 0将禁用当前过程中出现的任何错误捕获。

On Error GoTo -1 clears the error handling and sets it to nothing which allows you to create another error trap.

在错误后,清除错误处理并将其设置为零,这将允许您创建另一个错误陷阱。

Example: On Error GoTo -1

示例:错误上的GoTo -1

After the first error is raised, it will GoTo ErrorFound which will then clear the routine's error handling and set a new one, which will GoTo AnotherErrorFound when an error is found.

在第一个错误被提出之后,它将转到ErrorFound,然后清除例程的错误处理并设置一个新的错误,当发现错误时,它将转到另一个错误。

Sub OnErrorGotoMinusOneTest()

    On Error GoTo ErrorFound

    Err.Raise Number:=9999, Description:="Forced Error"

    Exit Sub

ErrorFound:

    On Error GoTo -1 'Clear the current error handling
    On Error GoTo AnotherErrorFound 'Set a new one
    Err.Raise Number:=10000, Description:="Another Forced Error"

AnotherErrorFound:

    'Code here

End Sub

Example: On Error GoTo 0

示例:出错后转到0

After the first error is raised, you will receive the error as error handling has been disabled.

在第一个错误被提出后,您将收到错误,因为错误处理已被禁用。

Sub OnErrorGotoZeroTest()

    On Error GoTo 0

    Err.Raise Number:=9999, Description:="Forced Error"

End Sub

#2


8  

This answer addresses the confusion between the error object and the error handler.

这个答案解决了错误对象和错误处理程序之间的混淆。

The error object can be cleared using Err.Clear. This does not affect the error handler.

可以使用error . clear清除错误对象。这不会影响错误处理程序。

The error handler becomes enabled by using On Error Goto <label>. It becomes active when an error occurs.

错误处理程序通过使用错误Goto

While the error handler is active, you can not assign a new error handler. On Error Goto <label> will have no effect. VBA simply ignores the attempt to assign a new error handler.

当错误处理程序处于活动状态时,您不能分配新的错误处理程序。在错误时,Goto

Using Err.Clear does not cancel the error handler.

用错。Clear没有取消错误处理程序。

Jumping to a different place in the code using Goto <label> does not cancel the error handler. Using Goto <label> in an error handling block can cause confusion and should be avoided. You might think the error handler is no longer active when in fact it is still active.

使用Goto

The effect of an active error handler is that you can not assign a new error handler. On Error Goto <label> will have no effect. VBA simply ignores the attempt to assign a new error handler. Any additional errors will be unhandled while the error handler is active.

主动错误处理程序的作用是不能分配新的错误处理程序。在错误时,Goto

The only way to exit an active error handler is:

退出活动错误处理程序的唯一方法是:

  1. Resume
  2. 的简历
  3. Resume Next
  4. 恢复下
  5. Resume <label>
  6. 简历 <标记>
  7. On error goto -1
  8. On error goto 1
  9. exit the procedure
  10. 退出程序

Using any one of these ways to exit the error handler will also clear the error object.

使用其中任何一种方法退出错误处理程序也将清除错误对象。

Excellent source: Pearson Error Handling In VBA Chip Pearson doesn't mention On error goto -1 in his article. To quote him:

极好的来源:皮尔森错误处理在VBA芯片皮尔逊没有提到错误goto -1在他的文章。引用他的话:

I deliberately did not include On Error GoTo -1 because it serves no real purpose and can lock up the entire Excel application unless used in exactly the right way. Yes, On Error GoTo -1 is syntactically valid, but it is like giving a gun to drunk teenager. Nothing good will come from it.

我故意不包含On Error GoTo -1,因为它没有真正的用途,并且可以锁定整个Excel应用程序,除非以正确的方式使用。是的,在错误上,GoTo -1在句法上是有效的,但这就像给醉酒的青少年一把枪。它不会带来任何好处。

You can also handle errors inline without using an error handler using the error object: MSDN Inline Error Handling

您还可以使用error对象(MSDN内联错误处理)处理内联错误,而无需使用错误处理程序

#3


2  

Here's another one

这是另一个

http://www.excelfox.com/forum/f23/error-goto-1-a-894/

http://www.excelfox.com/forum/f23/error-goto-1-a-894/

Shows a bit more on what's the difference between GoTo 0 and GoTo -1

再看一下GoTo 0和GoTo -1之间的区别。

#4


-1  

It is important to realise there are two distinct things that happen when an error occurs in VBA.

重要的是要认识到,当VBA中发生错误时,会发生两件截然不同的事情。

  1. The error object has it's properties set (ie err.number, err.desciption, err.source etc)

    错误对象具有其属性集(即err)。号码、err.desciption犯错。源等)

  2. The next line to be executed changes.
    Which line is executed is determined by the last "On Error Goto" statement that was executed - if any.

    要执行的下一行更改。执行哪一行取决于最后执行的“On Error Goto”语句——如果有的话。

These are separate but highly related topics and you will write what is in effect distinct but interwoven code to manage them both.

这些是独立的但高度相关的主题,您将编写实际不同但相互交织的代码来管理它们。

When ANY error occurs or you use Err.Raise the Err object is ALWAYS set up. Even if "On Error Resmue next" or any other On error statement has been used.

当出现错误或使用Err时。始终设置Err对象。即使使用了“On Error Resmue next”或任何其他On Error语句。

So code like this could ALWAYS be used:

所以像这样的代码总是可以使用的:

Dim i as integer 
On error resume next 
i = 100/0  ' raises error
if err.number <> 0 then 
   ' respond to the error
end if

It is really important to realise that when the error object has a non zero value for err.number an exception has been raised AND that if you then try and execute any "On Error Goto " statement doing so will raise an error and execution will be passed to any code that called the current procedure. (or where not called by any code the usual VBA error dialogue is given). Note that in this scenario "On Error Goto ALabel1" would NOT change the next line to be the line with Label1: on it.

当错误对象的值为err时,必须意识到这一点。出现了一个异常,如果您尝试执行任何“On Error Goto”语句,那么将会引发错误,执行将传递给调用当前过程的任何代码。(或者在没有被任何代码调用的地方,给出通常的VBA错误对话)。注意,在这个场景中,“On Error Goto ALabel1”不会将下一行更改为带有标签1的行:在上面。

eg

Sub ErrorTest()

    Dim dblValue        As Double

    On Error GoTo ErrHandler1
    dblValue = 1 / 0

ErrHandler1:
    debug.print "Exception Caught"
    debug.print Err.Number

    On Error GoTo ALabel1
    dblValue = 1 / 0

Exit sub
ALabel1:
    debug.print "Again caught it."

End Sub

Once the err.number property is set to non zero, you can reset it to zero by using

一旦犯错。number属性设置为non - zero,可以通过使用将其重置为zero

On Error Goto -1 

Note that Err.Clear also resets it to zero but it is actually equivalent to:

注意,犯错。Clear也将其重置为0,但它实际上等价于:

On Error Goto -1 
On Error Goto 0

ie Err.Clear removes an "On Error Goto" that is currently in place. So therefore it is mostly best to use:

ie犯错。Clear删除当前正在执行的“On Error Goto”。因此,最好用:

On Error Goto -1   

as using Err.clear You would often need to write

用错。显然你经常需要写作

Err.Clear
On Error Goto MyErrorHandlerLabel

It is worth noting that Err.Clear is implicitly carried out by VBA whenever it executes any type of Resume statement, Exit Sub, Exit Function, Exit Property, or any On Error statement.

这是值得注意的。每当执行任何类型的Resume语句、Exit Sub、Exit函数、Exit属性或任何On Error语句时,VBA都会隐式执行Clear。

You can also set the error object it to whatever number you like using

您还可以将error对象设置为您喜欢的任何数字

Err.Raise Number:=, Source:=, Description:=

犯错。提高数字:来源:=、描述:= =

Err.Raise is very important as it allows you to propagate an error to the calling program AND raise your own error numbers known as "user defined errors" that provide a means of telling the calling program that it could not continue for a logical reason. (eg a business rule was broken).

犯错。Raise非常重要,因为它允许您将错误传播到调用程序,并生成您自己的错误号,称为“用户定义的错误”,该错误号提供了一种方法,可以告诉调用程序由于逻辑原因无法继续。(例商业规则被打破)。

You can control which line of code is executed next using statements like

您可以使用下面的语句来控制执行哪一行代码

On Error Goto ALabelName On Error Goto ANonZeroLineNumber and On Error Goto 0 ' This is a special case as it in effect says "within the current scope (typically a sub or function), in the event that an error happens pass the error object back to the code that called the current sub or function.

On Error Goto ALabelName On Error Goto ANonZeroLineNumber On Error Goto 0这是一个特殊的例子,因为它实际上说:“在当前的范围内(通常是一个接头或函数),如果一个错误发生的错误对象传递回调用当前子或函数的代码。

Error handling in VBA is tricky, especially as the MSDN pages do not really give complete examples of how error handling can be used.

VBA中的错误处理很棘手,特别是MSDN页面并没有给出如何使用错误处理的完整示例。