Visual Basic 6 How to use Exceptions?

Viewed 579

I have looked all over the Internet for an answer that addresses exception handling in VB6 and can't find anything useful. I want to show an error to the user and halt execution of the program where it encountered that error, I don't want execution to continue.

I've looked into using End but this isn't what I'm looking for. End closes all forms and exits. I've also used Stop but that temporary suspends execution.

I'm looking to handle logic exceptions if possible with this version of Visual Basic. If exceptions aren't available, is there a way to emulate them?

1 Answers

Here is a template I use in all my projects:

Private Sub Test()
   On Error GoTo catch

   'your code which may cause an error

   'if you wanted to raise a user-defined error which start at 513
   Err.Raise vbObjectError + 513, "MyModule", "MyMessage"   

finally:
   'any code that always has to happen
   Exit Sub

catch:
   'logging, message boxes, etc, however you want to handle the error
   MsgBox Err.Number & ": " & Err.Description
   Resume finally
End Sub
Related