Error Handler - Exit Sub vs. End Sub

RNamo picture RNamo · Sep 4, 2009 · Viewed 133.2k times · Source

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.

Example:

Public Sub SubA()
On Error Goto ProcError

  ''# other code  
  MsgBox FuncA()

ProcExit:  
  Exit Sub

ProcError:  
  MsgBox Err.Description  
  Resume ProcExit
End Sub

Answer

AngryHacker picture AngryHacker · Sep 4, 2009

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