Best practice for error number assignments?

M

Mark Twombley

Hi, I'm just getting back into C++ and had a question about the best
practice for assigning error numbers.
I have been working in VB for sometime now and there you would start
assigning error number at vbObjectError + count.

Is there a similar practice in C++ or is it just coder preference.

Thanks
 
J

Jeff Schwab

Mark said:
Hi, I'm just getting back into C++ and had a question about the best
practice for assigning error numbers.
I have been working in VB for sometime now and there you would start
assigning error number at vbObjectError + count.

Is there a similar practice in C++ or is it just coder preference.


What do you mean by "error number?" Do you mean the status returned by
your program to the environment? For communicating errors withing your
program, you shouldn't need to set global integers like errno...
 
S

Samuele Armondi

Mark Twombley said:
Hi, I'm just getting back into C++ and had a question about the best
practice for assigning error numbers.
I have been working in VB for sometime now and there you would start
assigning error number at vbObjectError + count.

Is there a similar practice in C++ or is it just coder preference.

Thanks
--
Mark Twombley
diaganos(numeric)one at shaw dot ca
eg. (e-mail address removed)
"Say No to Spam"

It is usually best to use enum's so you are sure to get unique values, but
it is also worth remebering that exceptions are usually much better at
handling errors, especially as your programs get more complex.
HTH,
S. Armondi
 
M

Mark Twombley

Jeff Schwab said:
What do you mean by "error number?" Do you mean the status returned by
your program to the environment? For communicating errors withing your
program, you shouldn't need to set global integers like errno...

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.
 
J

Jeff Schwab

Mark said:
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.

"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.
 
M

Mark Twombley

Samuele Armondi said:
It is usually best to use enum's so you are sure to get unique values, but
it is also worth remebering that exceptions are usually much better at
handling errors, especially as your programs get more complex.
HTH,
S. Armondi

Thanks Samuele:
Is it a good practice to use blocks of numbers for specific sections or does
that just add more confusion.
 
M

Mark Twombley

Jeff Schwab said:
"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.
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.
 
C

Cy Edmunds

Mark Twombley said:
Hi, I'm just getting back into C++ and had a question about the best
practice for assigning error numbers.
I have been working in VB for sometime now and there you would start
assigning error number at vbObjectError + count.

Is there a similar practice in C++ or is it just coder preference.

Thanks
--
Mark Twombley
diaganos(numeric)one at shaw dot ca
eg. (e-mail address removed)
"Say No to Spam"

There is nothing in the C++ standard about what error numbers you can use.
If there is a constraint for your application it must come from someplace
else. When I need to construct error numbers I just make them up.
 
S

Samuele Armondi

Mark Twombley said:
Thanks Samuele:
Is it a good practice to use blocks of numbers for specific sections or does
that just add more confusion.
Personally, I think that it could add to the clarity of the program...but
try to avoid global variables like error_number and things like that from
cluttering up your code. Write an exception class and derive all the types
of exceptions you need from there, i.e.
class Exception
{
//
}

class HardwareException : public Exception
{
//
}

class OperatingSystemException : public Exception
{
//
}

class OtherException : public Exception
{
//
}

and so on... I think that would make everything clearer.
HTH,
S. Armondi
 
J

Jeff Schwab

Mark said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top