stickleprojects Level: Moderator

 Registered: 09-09-2002 Posts: 1027
|
Re: Error code structure
HI
VB6 doesn't lend itself well to shrink wrapped error-handling, but I follow this structure:
EVERY function/proc (routine) has an error handler that raises the error (and appends the routine's name to the source) or shows it.
If the routine will be at the top of a call stack (ie. is an event), then show the error.
Show the errors using a central ShowError routine, that copies the err object and displays a pretty form with an english text, the call stack, allows email to support, etc.
I use MZTools' excellent error template to insert an error handler into the existing proc (hot-key ctrl-shift-E) then comment out the show/raise line as applicable.
example:
function mythingy (a, b, c) as boolean
on error goto errh
' default return value
' check params
if a<b then goto exith
if c>b then goto exith
'do some code here
' never exit the middle of a routine, always goto the exith
mythingy = true
exith:
on error goto 0
' cleanup, error handling is off to stop looping on errh if i put a resume_next in later on
exit function
errh:
err.raise err.number, err.source & ",module1.bas:mythingy", err.description
' ShowError err, "module1.bas:mythingy","Creating invoice"
end function
|
I use functions rather than procs and return Success values just before the function ends. I see very little reason for fire-and-forget routines.
On top of this I add specific implementation for errors as required (ie. overwrite file prompts, readonly folders, etc.).
My style is defensive coding and I tend to assume that the calling programmer is an idiot who will not have checked the parameters before passing them in (ie. recordsets not open, objects nothing, etc).
NOTE:
This is just my structure and there are thousands of articles on different schemes. For example, I don't use asserts and never raise an error for an invalid argument.
Hope that's clear
Kieron
[Edited by stickleprojects on 15-05-2008 at 04:40 AM GMT]
____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)
|