error handler

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can someone help me? I have an error handler that is not working correct. Below is a snippet, for some reason it read through, all the way to the error handler though there is not one. Thanks.

Public Sub GetData()
On Error GoTo MyHandler

With myObject
msgbox "test"
'- - -- - more code here etc..
End With

MyHandler:
MsgBox "error"

end sub
 
You forgot to exit the Sub if there was no error. Just add an Exit Sub
before the error handling code.

Public Sub GetData()
On Error GoTo MyHandler

With myObject
msgbox "test"
'- - -- - more code here etc..
End With

Exit Sub ' ** add this here **

MyHandler:
MsgBox "error"

end sub

If you're using VB.NET you should *really* consider switching to structured
error handling using try/catch/finally blocks. If you decided to do this
your code would be changed to:

Public Sub GetData()
Try
With myObject
MsgBox "test"
'- - -- - more code here etc..
End With
Catch ex As Exception
MsgBox "Error: " & ex.Message
End Try
End Sub

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada



anonymous said:
Can someone help me? I have an error handler that is not working correct.
Below is a snippet, for some reason it read through, all the way to the
error handler though there is not one. Thanks.
 
Back
Top