何时合适地显式使用Err.Clear? [英] When is it appropriate to explicitly use Err.Clear?

查看:52
本文介绍了何时合适地显式使用Err.Clear?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,以下功能用于检查工作簿是否打开:

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

这两个 Err.Clear 语句是否必要?

推荐答案

在此示例中

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

没有一种用法是适当的,因为 On Error 会重置最后一个错误,所以 Err.Clear 是多余的.

none of the uses is appropriate, because On Error resets the last error, so Err.Clear is redundant.

实际上是在处理一条失败的语句之后.

It's appropriate after actually handling a 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

如果有效的是 On Error Resume Next ,该程序将在发生错误后继续运行,就好像什么都没有发生一样.没有抛出异常,没有警告,这不是结构化的错误处理(即没有什么像 try / catch 块).如果您不进行严格的错误检查,则您的程序可能会以非常奇怪的状态结束.

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.

这意味着您必须在之后检查错误.每一个.陈述.那.能够..如果Err.Number<>错误,请准备好写很多.0然后检查.请注意,这比看起来更难解决.

This means you must check errors after. every. statement. that. can. fail. Be prepared to write a lot of If Err.Number <> 0 Then checks. Note that this is harder to get right than it seems.

更好的是:避免像瘟疫一样,避免长时间使用具有有效的<错误恢复下一个错误的代码.将操作分解为仅执行一件事的较小的函数/子程序,而不是编写可以完成所有操作但可能会中途失败的大型函数.

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.

简而言之: Err.Clear 使您的程序在 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.

当然,在您的示例中,通过使用公认的检查工作簿(即集合的成员)是否存在的方式,很容易避免错误处理.

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

这篇关于何时合适地显式使用Err.Clear?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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