Solved

Efficient way of defining error codes

Posted on 2010-09-14
9
527 Views
Last Modified: 2012-06-27
I am currently working on a big existing software architecture in C++. It has error codes in the header file defined something like this:

static const int ERROR_ABC -1;
static const int ERROR_PQR -2;
static const int ERROR_XYZ -3;
.
.
.
.


Since -1, -2, etc values are clashing with some numerical results after the code was modified recently, I have been asked to come up with a more efficient method to handle error codes. Please give your suggestions (preferably with examples) to solve this problem. One idea I got was to change -1, -2 etc in the header file to something bigger like 100001, 100002, etc. Not sure if this is really a good way.
0
Comment
Question by:sukhoi35
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
ID: 33671872
It depends on the range of acceptable values. If you use the same variable to store the result or the error code, then the error codes have to be outside of the range of valid values (obviously). Which ones you choose is up to you - one is not more efficient than the other.

A few notes though :

(a) why not use enums for defining error codes ?
(b) in cases where the valid values clash with the defined error codes, why not keep the error code separate from the result ? In other words, let the function return an error code (0 for success), and pass the result back via a function parameter.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 300 total points
ID: 33671882
How about:    enum Err_Code { ERROR_ABC = 0xFFAA, ERROR_PQR = 0xFFBB, ERROR_XYZ = 0xFFCC } err;>> Since -1, -2, etc values are clashing with some numerical results Not sure I understand how you got error codes mixed up with other numerical results, but maybe this extreme example will help.
class ERROR_CODE {
public:
   enum Err_Code {
       ERROR_ABC = 0xFFAA,
       ERROR_PQR = 0xFFBB,
       ERROR_XYZ = 0xFFCC
   } err;
};

int main() {
   ERROR_CODE::Err_Code err_status;
   err_status = ERROR_CODE::ERROR_ABC;
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 33671888
what results are they clashing with, and how are those results obtained?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Assisted Solution

by:w00te
w00te earned 100 total points
ID: 33671901
Make your errors into an enumeration, and start the enumeration values equal to whatever high number you want.  After explicitly definign the first enumeration value, the rest of the enumerations will progress from there by default.

enum ErrorCodes
{
    ERROR_ABC = 100001,
    ERROR_PQR,
    ERROR_XYZ
};

When you want to print the codes, you can just std::cout<<ERROR_ABC; or the containing variable.

-w00te
0
 
LVL 32

Accepted Solution

by:
phoffric earned 300 total points
ID: 33672520
Revision of code:1) Used static2) Added namespace exampleThe idea is to limit what assignment operations can be made, and hopefully avoid numeric clashes.
namespace ERR_CODE {
   static enum Err_Code { ERROR_ABC = 0xFFAA, ERROR_PQR = 0xFFBB, ERROR_XYZ = 0xFFCC };
}

class ERROR_CODE {
public:
   static enum Err_Code { ERROR_ABC = 0xFFAA, ERROR_PQR = 0xFFBB, ERROR_XYZ = 0xFFCC };
};

int main() {
   ERROR_CODE::Err_Code err_status;
   ERR_CODE::Err_Code   err_code_stat;
   err_status = ERROR_CODE::ERROR_ABC;
   err_code_stat = ERR_CODE::ERROR_ABC;

   int val = err_code_stat;  // ok, but not desirable
   err_code_stat = val;      // compiler error (good)

   val = err_status;         // ok, but not desirable
   err_status = val;         // compiler error (good)
}

Open in new window

0
 

Author Comment

by:sukhoi35
ID: 33676639
Hi Experts,
Thank you very much for all your responses. The main reason for me to do this little refactoring of error handling is that the current code is kind of old and some changes were done to fit a customer requirement.

static const int ERROR_ABC -1;
static const int ERROR_PQR -2;
static const int ERROR_XYZ -3;

void func()
{
           nRet = callFunc();

           if(nRet==ERROR_ABC)
                 return nRet;
          else if(nRet==ERROR_PQR)
                  return nRet;
          else
          return nRet;
}

In the earlier code callFunc() returned a min value of 0 only. Because of some unplanned modifications that were done, now the minimum value that it can return is -1 which incidentally equals ERROR_ABC.

Will award points and close the question soon
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33676724
The example code you gave does not make much sense. You're always returning nRet, so you might as well have left out all checks :

        int func() {
            return callFunc();
        }

which still seems unnecessary. But I assume a few things got lost when you posted that code here.



>> Because of some unplanned modifications that were done, now the minimum value that it can return is -1

I think the advice already given covers that :)
0
 

Author Comment

by:sukhoi35
ID: 33680368
Yes you are right, I have not really pasted the original code here (since I am not supposed to) but typed it myself in a hurry. so, many things are not there above.

I anyway got the solution, so am closing the question. Thanks Everyone  :)
0
 

Author Closing Comment

by:sukhoi35
ID: 33680388
Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 309
trigs fail! I thought I knew how to do trignometry 3 92
Finding Divisors 5 60
MS Excel duplicate input detect. 8 44
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question