如何使用 On Error GoTo?
How do I use On Error GoTo?
我相信 vba IDE 不允许正确中断错误。我在这里问了一个关于如何识别运行时错误的问题:
解决方案/解决方法似乎是使用 On Error GoTo ErrorHandler
或类似方法。我正在努力做到这一点,但没有取得太大的成功。
根据 Microsoft On Error GoTo
将在发生运行时错误 (https://msdn.microsoft.com/en-us/library/5hsw66as.aspx) 时将您发送到指定的区域代码。根据我的经验,这还不是全部。这个问题是关于如何使该解决方案真正起作用。
所以我有以下功能:
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 告诉我它实际上在那条线上掉了下来,而无需我手动跳过代码。
我在之前尝试解决所有错误时启用了该功能 "Break on all Errors"。
我通过转到 Tools->Options->General
并将 Break on All Errors
替换为 Break on Unhandled Errors
来关闭此功能。
原来Break on All Errors
对于"Break on Some Errors, but just return garbage if it's a UDF error"是Windows。
我刚刚测试过,你的错误处理程序工作正常,它在
上正确中断
Debug.Assert False
--> 检查调试器选项。
然而,这不是处理 vBA 中错误的正确方法,因为如果你编译你的应用程序而忘记调整这样的错误处理程序,当用户在程序中遇到错误时,应用程序将陷入死循环。
在大多数情况下,您必须抛出一个消息框让用户知道发生了错误,并终止当前过程。唯一不适用的情况是当您执行可能触发错误但您知道并故意绕过该错误的操作时。这种情况很少见。
我从来没有使用过Assert
方法,这就是我在每个过程中处理错误的方式
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
我相信 vba IDE 不允许正确中断错误。我在这里问了一个关于如何识别运行时错误的问题:
解决方案/解决方法似乎是使用 On Error GoTo ErrorHandler
或类似方法。我正在努力做到这一点,但没有取得太大的成功。
根据 Microsoft On Error GoTo
将在发生运行时错误 (https://msdn.microsoft.com/en-us/library/5hsw66as.aspx) 时将您发送到指定的区域代码。根据我的经验,这还不是全部。这个问题是关于如何使该解决方案真正起作用。
所以我有以下功能:
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 告诉我它实际上在那条线上掉了下来,而无需我手动跳过代码。
我在之前尝试解决所有错误时启用了该功能 "Break on all Errors"。
我通过转到 Tools->Options->General
并将 Break on All Errors
替换为 Break on Unhandled Errors
来关闭此功能。
原来Break on All Errors
对于"Break on Some Errors, but just return garbage if it's a UDF error"是Windows。
我刚刚测试过,你的错误处理程序工作正常,它在
上正确中断Debug.Assert False
--> 检查调试器选项。
然而,这不是处理 vBA 中错误的正确方法,因为如果你编译你的应用程序而忘记调整这样的错误处理程序,当用户在程序中遇到错误时,应用程序将陷入死循环。
在大多数情况下,您必须抛出一个消息框让用户知道发生了错误,并终止当前过程。唯一不适用的情况是当您执行可能触发错误但您知道并故意绕过该错误的操作时。这种情况很少见。
我从来没有使用过Assert
方法,这就是我在每个过程中处理错误的方式
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