本文介绍了错误处理程序 - 退出子对比结束子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我想退出一个错误处理程序(在处理后)使用Exit Sub,而不是让它转到End Sub?

Why would I want to get out of an Error Handler (after handling) with an Exit Sub instead of just letting it go to the End Sub?

确定它很简单。我只是不明白感谢任何帮助。

I'm sure it's simple. I just don't understand. Thanks for any help.

示例:

Public Sub SubA()
On Error Goto ProcError

  ''# other code
  MsgBox FuncA()

ProcExit:
  Exit Sub

ProcError:
  MsgBox Err.Description
  Resume ProcExit
End Sub


推荐答案

您的ProcExit标签是您发布所有资源的地方,不管是否发生错误。例如:

Your ProcExit label is your place where you release all the resources whether an error happened or not. For instance:

Public Sub SubA()
  On Error Goto ProcError

  Connection.Open
  Open File for Writing
  SomePreciousResource.GrabIt

ProcExit:
  Connection.Close
  Connection = Nothing
  Close File
  SomePreciousResource.Release

  Exit Sub

ProcError:
  MsgBox Err.Description
  Resume ProcExit
End Sub

这篇关于错误处理程序 - 退出子对比结束子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:54