Mark Twombley wrote:
I meant unique identifiers for error conditions in functions. I'm
working on a communications application right now. It was
originally done in VB and I'm working on making it a service to run
on W2K or better. I've noticed some exceptions to the
communications cycle. I want to record that an error (exception)
occurred to a log. When one of my functions returns it may not have
received a reply from the device as expected. The application
doesn't need to stop but I want to record that something went wrong
so that if something else fails later in the cycle I can then look
at the log and see if there is a pattern to the problem.
Jeff Schwab wrote:
"Exception" has an established meaning in C++, but it doesn't sound
like that's what you mean. Will the "error numbers" appear only in
log files? If so, you're free to assign whatever numbers you like
(there is no standard convention). If you're planning to use the
numbers to communicate errors among different parts of your program,
there are different approaches available.
Mark Twombley wrote:
I have worked out the code in a sample application for recording to the
event log in windows. What are your suggestions for communicating
errors among different parts of my application. If one function didn't
do what was expected I return FALSE and set a variable LAST_ERROR to an
error code so the calling function can do what ever is needed to handle
the error. Sometimes the error isn't severe so nothing needs to be
done, I just want a record of it.
When you have an error that requires an interruption to the normal order
of your program, you can "throw an exception" up the call stack, and
"catch" it at whatever level seems appropriate. The stack is unwound
for you automatically. This is sort of like C's setjmp/longjmp
facility, but the stack is unwound correctly, so destructors are called
and resources can be released. Also, because exceptions give you an
alternative to the normal way of unwinding the stack, you don't have to
check for errors after each function call. If an error occurs, the code
you provide for handling it will be invoked automatically.
Like pretty much everything else in C++, exceptions are based on the
language's types. Instead of specifying different a different block of
code to execute for each error number (as with a switch(errno)
statement), you specify a different block for each type of exception.
Thanks to multiple inheritance, a given exception can be caught by any
of several different handlers, each of which catches a reference to a
different base class of the exception.
If you have a good book on C++ (like _The C++ Programming Language_
Special Edition, by Stroustrup), look up exceptions, and particularly
the throw and catch keywords.
Good luck,
Jeff