Error Handler - Exit Sub vs. End Sub

前端 未结 2 692
眼角桃花
眼角桃花 2021-01-07 18:20

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\'

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-07 18:43

    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
    

提交回复
热议问题