All Snippets
Top 100 Snippets

By Language

GBIC >> Source Code >> Visual Basic >> Snippet

20 Error handling

'Uncaught errors will halt execuation of VB code (crash)
'To catch  (handle) and error use on of these code techniques

'When an error occurs, VB sets the err object, which has these properties
'these are reset after Exit Sub, Exit Function, Exit Property  or Resume Next statements
.Clear    'clears the err object
.Description   'text description of the error
.HelpContext   'context ID for a topic in a Help file. Read/write.
.HelpFile   'full path name of Help file
.LastDllError   'return value of last DLL call where an error was returned
.Number   'number assigned to this error (default property)
.Raise    'use in run-time to generate an error for testing error-handling code
.Source   'name of object that generated the error

'Enable error trapping this way:
On Error Goto  Label
... normal code goes here
Label:     'this is where execution branches to when an error occurs
.... Error - handling code goes here

'To get out of an error-handling routine, use one of these
Resume [0]   'repeats the error-causing statement (use when the cause of error has been corrected)
Resume Next  'resumes at the statement immediately following the one that caused the error
Resume line   'resumes at the specified label (must be in same procedure as the error handler)
'Generally, use Resume[0] when the error gets corrected and Resume Next when it does not

'To disable error trapping use this line of code:
On Error Goto 0

'The VB IDE has special tools for debugging/controlling errors
'Breakpoints  lines of code there the IDE pauses execution
'Step Into   execute next line of code, including when the next line is in a procedure
'StepOver   execute next line of code, without stepping into a procedure
'StepOut   executes the current procedure and stops at the next line in the calling procedure
'Locals Windows  displays the current value of local variables
'Immediate Window run code while application is in break mode
'Watch Window  displays value of selected expressions
'Quick Watch  list current value of expression while application is in break mode
'Call Stack   in break mode, lists all procedures that have been called but not yet run to completion