473,545 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best practice for error number assignments?

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(numeri c)one at shaw dot ca
eg. bo**@nowhere.co m
"Say No to Spam"
Jul 22 '05 #1
9 2612
"Mark Twombley" <ju**@shaw.ca > wrote in message
news:oHlIb.8732 25$6C4.102906@p d7tw1no...
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(numeri c)one at shaw dot ca
eg. bo**@nowhere.co m
"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
Jul 22 '05 #2
Mark Twombley wrote:
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...
Jul 22 '05 #3
"Jeff Schwab" <je******@comca st.net> wrote in message
news:hI******** ************@co mcast.com...
Mark Twombley wrote:
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...


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.
Jul 22 '05 #4
"Samuele Armondi" <sa************ ****@hotmail.co m> wrote in message
news:3f******** **@mk-nntp-1.news.uk.world online.com...
"Mark Twombley" <ju**@shaw.ca > wrote in message
news:oHlIb.8732 25$6C4.102906@p d7tw1no...
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(numeri c)one at shaw dot ca
eg. bo**@nowhere.co m
"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


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

Jul 22 '05 #5
Mark Twombley wrote:
"Jeff Schwab" <je******@comca st.net> wrote in message
news:hI******** ************@co mcast.com...
Mark Twombley wrote:
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...

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.
Jul 22 '05 #6
"Jeff Schwab" <je******@comca st.net> wrote in message
news:h4******** ************@co mcast.com...
Mark Twombley wrote:
"Jeff Schwab" <je******@comca st.net> wrote in message
news:hI******** ************@co mcast.com...
<< SNIP>>

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.

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.
Jul 22 '05 #7
"Mark Twombley" <ju**@shaw.ca > wrote in message
news:oHlIb.8732 25$6C4.102906@p d7tw1no...
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(numeri c)one at shaw dot ca
eg. bo**@nowhere.co m
"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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #8

"Mark Twombley" <ju**@shaw.ca > wrote in message
news:ZcmIb.8809 80$9l5.288860@p d7tw2no...
"Samuele Armondi" <sa************ ****@hotmail.co m> wrote in message
news:3f******** **@mk-nntp-1.news.uk.world online.com...
"Mark Twombley" <ju**@shaw.ca > wrote in message
news:oHlIb.8732 25$6C4.102906@p d7tw1no...
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(numeri c)one at shaw dot ca
eg. bo**@nowhere.co m
"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


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 HardwareExcepti on : public Exception
{
//
}

class OperatingSystem Exception : public Exception
{
//
}

class OtherException : public Exception
{
//
}

and so on... I think that would make everything clearer.
HTH,
S. Armondi
Jul 22 '05 #9

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

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2015
by: John Pastrovick | last post by:
I am not sure what the most effective way to organize a db for users is. I have 40 users (teachers) and 20 tables (grades, assignments, students, etc). Each needs access to its OWN grades, assignemnts, etc. NOW I am not sure how to orgainize users in the DB. Right now I have the code for 1 user and need to adapt it to several users.
2
1558
by: John Pastrovick | last post by:
I am not sure what the most effective way to organize a db for users is. I have 40 users (teachers) and 20 tables (grades, assignments, students, etc). Each needs access to its OWN grades, assignemnts, etc. NOW I am not sure how to orgainize users in the DB. Right now I have the code for 1 user and need to adapt it to several users.
131
21536
by: Peter Foti | last post by:
Simple question... which is better to use for defining font sizes and why? px and em seem to be the leading candidates. I know what the general answer is going to be, but I'm hoping to ultimately get some good real world examples. Fire away! :) Regards, Peter Foti
136
9213
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to...
3
1608
by: Paul | last post by:
Hi all. Can someone provide some help on the following as there seems to be many different methods of acheiving the same outcome. Basically I am trying to develop a web service which will spawn an exe file to run an import and then return a result back to the web service, this would also need the facility to spawn multiple copies of the...
16
2778
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and efficient navigating within Visual Studio 2005. Let's say your project (or solution) has dozens of forms and hundreds or even thousands of routines. ...
3
2577
by: HP | last post by:
Hi there The problem of dynamically created controls vs viewstate is widely known one. To access values of controls they have to be recreated on Page_Load. Unfortunately it causes many problems in the following (rather common, I guess) scenario: The controls are created dynamically. Their number and contents
5
2459
by: =?Utf-8?B?QmlsbHk=?= | last post by:
asp.net 2.0 vs2005 What is best practice for exception handling on a website in vs2005? I was going to catch errors in application on_error event, log them to the event log, then send users to an error page. But looking at the event log, asp.net 2.0 logs those errors anyways, with more detail than i cen get from the exception object. So is...
2
5622
by: Ben Joyce | last post by:
Hi all. I'm confused as to what the best or expected approch is to Web Service design under .Net, mainly with regards to Methods and Parameters. This is a bit awkward to explain so please bear with me. I have a web service that needs to be accessed via GET, POST and SOAP. The method expects an interger, a CustomerId for example, does...
0
7484
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7440
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5997
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1902
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.