“On Error Goto 0”退出功能之前 [英] "On Error Goto 0" before Exit Function

查看:731
本文介绍了“On Error Goto 0”退出功能之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MzTools中的默认错误处理程序模板是:

The default error handler template in MzTools is:

On Error GoTo {PROCEDURE_NAME}_Error

    {PROCEDURE_BODY}

   On Error GoTo 0
   Exit {PROCEDURE_TYPE}

{PROCEDURE_NAME}_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure {PROCEDURE_NAME} of {MODULE_TYPE} {MODULE_NAME}"


$ b $的程序{PROCEDURE_NAME}中的(& Err.Description&) b

请注意, On Error Goto 0 直接放在Exit语句之前。这是什么意思?这似乎是多余的。当函数退出时,该函数的错误处理程序将关闭。

Note that On Error Goto 0 is placed directly prior to the Exit statement. What is the point of doing this? It seems redundant. The error handler for the function will turn off anyway when the function exits.

推荐答案

错误goto 0 在当前范围内关闭错误处理,并恢复为默认情况。

on error goto 0 turns off error handling in the current scope and reverts to the default case.

在这种情况下,希望错误处理程序在该功能的持续时间内处于活动状态,但希望在退出时将其关闭。正如你所说,在这种情况下不需要,因为错误处理程序的范围是 {PROCEDURE_TYPE} ,所以它将在退出时还原。

In this instance, the procedure wants an error handler to be active for the duration of the function but it wants it turned off upon exit. As you say, it's not necessary in this case since the scope of the error handler is the {PROCEDURE_TYPE} so it will be reverted on exit.

但是有些时候,编码器(特别是那些生成模板的人)喜欢自己清理后即使在技术上不需要,比如在退出之前释放C程序中的分配内存。

But sometimes, coders (especially those that generate templates) like to clean up after themselves even when not technically necessary, such as freeing allocated memory in a C program before exiting.

这是我怀疑的情况,这真的是只是假设。

That's what I suspect is the case here, that it really is just supposition.

这篇关于“On Error Goto 0”退出功能之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆