Error Handler - Exit Sub vs. End Sub Error Handler - Exit Sub vs. End Sub vba vba

Error Handler - Exit Sub vs. End Sub


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.GrabItProcExit:    Connection.Close  Connection = Nothing  Close File  SomePreciousResource.Release  Exit SubProcError:    MsgBox Err.Description    Resume ProcExitEnd Sub


Typically if you have database connections or other objects declared that, whether used safely or created prior to your exception, will need to be cleaned up (disposed of), then returning your error handling code back to the ProcExit entry point will allow you to do your garbage collection in both cases.

If you drop out of your procedure by falling to Exit Sub, you may risk having a yucky build-up of instantiated objects that are just sitting around in your program's memory.