vba 什么时候显式使用 Err.Clear 合适?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19477375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When is it appropriate to explicitly use Err.Clear?
提问by Felix
For example, the following function is used for checking whether a workbook is open:
例如,以下函数用于检查工作簿是否打开:
Function BookOpen(Bk As String) As Boolean
Dim T As Excel.Workbook
Err.Clear
On Error Resume Next
Set T = Application.Workbooks(Bk)
BookOpen = Not T Is Nothing
Err.Clear
On Error GoTo 0
End Function
Are these two Err.Clear
statements necessary?
这两个Err.Clear
说法有必要吗?
回答by Tomalak
In this example
在这个例子中
Function BookOpen(Bk As String) As Boolean
Dim T As Excel.Workbook
Err.Clear
On Error Resume Next
Set T = Application.Workbooks(Bk)
BookOpen = Not T Is Nothing
Err.Clear
On Error GoTo 0
End Function
none of the uses is appropriate, because On Error
resets the last error, so Err.Clear
is redundant.
没有一个用途是合适的,因为On Error
重置最后一个错误,所以Err.Clear
是多余的。
It's appropriate after actually handlinga failed statement.
在实际处理失败的语句之后是合适的。
Function BookOpen(Bk As String) As Boolean
Dim T As Excel.Workbook
On Error Resume Next
Set T = Application.Workbooks(Bk) ' this can fail...
' so handle a possible failure
If Err.Number <> 0 Then
MsgBox "The workbook named """ & Bk & """ does not exist."
Err.Clear
End If
BookOpen = Not T Is Nothing
End Function
If you have On Error Resume Next
in effect, the program will continue after an error as if nothing had happened. There is no exception thrown, there is no warning, this is not structured error handling (i.e. it's nothing like try
/catch
blocks). Your program might end up in a very weird state if you don't do rigorous error checks.
如果你已经On Error Resume Next
生效,程序会在出错后继续运行,就好像什么都没发生过一样。没有抛出异常,没有警告,这不是结构化错误处理(即它不像try
/catch
块)。如果您不进行严格的错误检查,您的程序最终可能会处于非常奇怪的状态。
This means you must check errors after. every. statement. that. can. fail.Be prepared to write a lotof If Err.Number <> 0 Then
checks. Note that this is harder to get right than it seems.
这意味着您必须在之后检查错误。每一个。陈述。那。能够。失败。准备写了很多的If Err.Number <> 0 Then
检查。请注意,这比看起来更难做到。
Better is: Avoid long sections of code that have On Error Resume Next
in effect like the plague. Break up operations into smaller functions/subs that do only one thing instead of writing a big function that does it all but can fail halfway through.
更好的是:避免On Error Resume Next
像瘟疫一样有效的长代码段。将操作分解为只做一件事的较小的函数/子函数,而不是编写一个完成所有工作但可能中途失败的大函数。
In short: Err.Clear
makes your program behave predictably after a failed statement in an On Error Resume Next
block. It marks the error as handled. That's its purpose.
简而言之:Err.Clear
使您的程序在On Error Resume Next
块中的语句失败后具有可预测的行为。它将错误标记为已处理。这就是它的目的。
Of course in your sample it's easy to avoid error handling by using the commonly accepted way of checking whether a workbook (i.e. member of a collection) exists.
当然,在您的示例中,通过使用普遍接受的检查工作簿(即集合的成员)是否存在的方式,很容易避免错误处理。
Function BookOpen(Bk As String) As Boolean
Dim wb As Variant
BookOpen = False ' not really necessary, VB inits Booleans to False anyway
For Each wb In Application.Workbooks
If LCase(wb.Name) = LCase(Bk) Then
BookOpen = True
Exit For
End If
Next
End Function