Re: Error Raising and Memory in VB (general question)
From: Vlad Kozin (VladKozin_at_hotmail.com)
Date: 02/13/04
- Next message: sr: "Re: images, where to find"
- Previous message: JohnK: "Re: images, where to find"
- In reply to: Abelardo Vacca: "Re: Error Raising and Memory in VB (general question)"
- Next in thread: Mark Alexander Bertenshaw: "Re: Error Raising and Memory in VB (general question)"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 13 Feb 2004 23:56:59 GMT
"Abelardo Vacca" <anonymous@discussions.microsoft.com> wrote in message
news:A93DF841-5AA4-454A-8BFD-D48722178F52@microsoft.com...
> Thanks for the responses,
>
> So Vlad, what is your standard to hanlde errors? I mean, with skipping the
memory releasing , recordset closing etc.. at the end of each function.
>
> My questions arises because by standard in my company, our components
should return errors indicating the exact function and class module where
they ocurred (never display it, unless we are UI). We achieve this normally
by returning an object error, or lately, an XML structure. We create it
where the error occurs (all functions handle errors), and return it layer by
layer (function by function) until it leaves the component by one of its
interface functions. It works, but I just find it kind of un-clean,
difficult-to-read-code, since we have to check if an error is returned each
time a user defined function is called.
>
> I thought it'd be more elegant simply to raise the error anywhere and keep
one single error handle per interface function. However the memory issue
arose and that's when I wrote on this forum.
>
> Do you know any references where I could get standards or strategies for
error handling that do not require display, but instead return the error?
>
> Thanks a lot
>
Returning an error object is an interesting idea.
Knowing that VB appears to be ineffective with objects, I prefer to work
with errors a bit differently.
I try to design components so the object methods return Long value, this is
either 0& (I called it ERR_SUCCESS), or the actual error number. M$ calls it
Windows-API style of error handling.
On the function level, the general rule I follow is to avoid Exit Function
or Exit Sub statements before the ErrHand line.
Most of my functions look similar to this:
public function DoSomething() as Long
on error goto ErrHand
' main stuff goes here
' you can call Err.Raise anywhere here, it will use the ErrHand handler
...
ErrHand:
if Err then
modError.MsgBoxLog Err.Number, Err.Description, vbExclamation,
gFunctionName
DoSomething = Err.Number
Err.Clear
else
DoSomething=ERR_SUCCESS
end if
' depending on the context we sometimes use On Error Resume Next here
' function local CleanUp stuff goes here
...
end function
Functions that do not return plain Long (like those returning object
references or custom types) are covered by a public GetLastError method.
Also, you can always define an OnError event for your class.
Also each project includes a module, say modError.bas. It contains public
functions GetErrorDescription and MsgBoxLog, along with a bunch of custom
error numbers declared as Long constants and their String-type descriptions.
(we work with Domino.Doc, and having all product-specific errors declared
was a necessity)
GetErrorDescription function is trivial, it returns a text description of
the error when given with the error number. It delivers either a
product-specific error description, or the system (or rather, VB-runtime)
error description when given with one of the system errors.
MsgBoxLog is a more interesting thing, it is declared almost exactly as the
standard MsgBox function, except it behaves differently depending on the
type of the application.
It is also controlled by conditional compilation directives (#if #else
#endif). Depending on the app type it either displays a messagebox (when in
UI), writes to the Event Log (unattended server-side app), or writes to a
table, etc.
So, my error handling 'standard' is a combination of error handling in
functions plus modError module, plus a couple of conditional compilation
switches.
If you want to look at the M$ recommendations on the subject, go to "Topics
on Handling errors" in the VB part of MSDN.
regards,
Vlad
- Next message: sr: "Re: images, where to find"
- Previous message: JohnK: "Re: images, where to find"
- In reply to: Abelardo Vacca: "Re: Error Raising and Memory in VB (general question)"
- Next in thread: Mark Alexander Bertenshaw: "Re: Error Raising and Memory in VB (general question)"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|