错误处理程序 - Exit Sub 与 End Sub [英] Error Handler - Exit Sub vs. End Sub

查看:18
本文介绍了错误处理程序 - Exit Sub 与 End Sub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我要使用 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

这篇关于错误处理程序 - Exit Sub 与 End Sub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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