如何使用“出错时转到"? [英] How do I use On Error GoTo?

查看:56
本文介绍了如何使用“出错时转到"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被认为是vba IDE不允许适当地打破错误.我问了一个有关如何在此处识别运行时错误的问题:

I'm lead to believe that the vba IDE doesn't allow proper breaking on errors. I asked a question on how to identify runtime errors here:

如何打破错误?

解决方案/解决方法似乎是使用On Error GoTo ErrorHandler或类似方法.我正在努力进行这项工作,但并没有取得太大的成功.

The solution / workaround seems to be to use On Error GoTo ErrorHandler or similar. I'm trying to make that work but haven't had much success.

根据Microsoft On Error GoTo,当发生运行时错误时,会将您发送到指定的区域代码( https://msdn.microsoft.com/en-us/library/5hsw66as.aspx ).根据我的经验,这还不是全部.这个问题是关于如何使该解决方案真正起作用的.

According to Microsoft On Error GoTo will send you to the specified code of region when a runtime error occurs (https://msdn.microsoft.com/en-us/library/5hsw66as.aspx). Based on my experience, that's not the whole story. This question is about how to actually make that solution work.

所以我有以下功能:

Function generateTimeseries() As Variant

On Error GoTo ErrorHandler

Dim currentArray As Range

currentArray = Selection.Range ' this doesn't work - I don't care why

generateTimeseries = currentArray.Rows

Return

ErrorHandler:
    Debug.Assert False
    Resume ' break here if you want to catch errors

End Function

此功能从不输入ErrorHandler代码.相反,它落在Selection.Range上,该函数仅返回#VALUE.我实际上并不在乎为什么会中断,我只想知道如何让IDE告诉我它实际上已经落在那条线上,而无需我手动跳过代码.

This function never enters the ErrorHandler code. Instead, it falls over on Selection.Range, with the function simply returning #VALUE. I don't actually care why this breaks, I just want to know how I can get the IDE to tell me that it actually fell over on that line, without me jumping through the code manually.

推荐答案

我刚刚测试了您的错误处理程序,它在

I just tested and your error handler works, it breaks correctly on

Debug.Assert False

->检查调试器选项.

但是,这不是处理vBA中错误的正确方法,因为如果您编译应用程序而忘记改编此类错误处理程序,则当用户在过程中遇到错误时,应用程序将陷入无限循环.

However this is not a correct way to handle errors in vBA, because if you compile your application and forget to adapt such error handler, when a user will encounter an error in the procedure, the application will fall into an infinite loop.

在大多数情况下,您必须抛出msgbox来让用户知道发生了错误,然后终止当前过程.唯一不适用此情况的情况是,您执行某些操作可能会触发错误,但您知道该错误并故意要绕过该错误.这是一种罕见的情况.

In mostly all situations, you have to throw a msgbox to let the user know that an error has occurred, and terminate the current procedure. The only situation where this is not applicable is when you do something that can trigger an error but you know it and deliberately want to bypass the error. It's a rare situation.

我从未使用过Assert方法,这就是我在每个过程中处理错误的方式

I have never used Assert method and this is how I handle my errors in every procedure

Function generateTimeseries() As Variant

    On Error GoTo ErrorHandler

    Dim currentArray As Range

    currentArray = Selection.Range ' this doesn't work - I don't care why

    generateTimeseries = currentArray.Rows

Exit_Function:
       Exit Function

ErrorHandler:
        MsgBox Err.Description, vbCritical, "Error " & Err.Number
        Resume Exit_Function ' breakpoint here if you want examine the code after error, otherwhise it terminates the function
        Resume ' Once  the code breaks on the above line, move to this instruction and F8 to return to the statement in error

End Function

这篇关于如何使用“出错时转到"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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