C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Error reporting design for....C... Posted by Gregry2 on 5 Mar 2007 at 6:05 AM
Sorry, no exceptions and throws and catches here...

Since my program is in C, I can't use fancy C++ features to report errors to callers of the functions I use, so instead, I decided on a common return value set. Each function returns one value from this set:

#define SINT_SUCCESS        0/*returned on successful operation*/
#define SINT_ERR_ARG        1/*one or more of the arguments passed are faulty*/
#define SINT_ERR_DYNMEM     2/*there was an error in dynamic allocation*/
#define SINT_ERR_TIDY       3/*removal of excess bytes in the output failed. This is not fatal*/
#define SINT_ERR_IO_BADBASE 4/*the base passed to an IO function is not supported*/
...



Now there is a weakness in this system. Only one error can be reported for each call. Lets say we have some number of errors, we need to be able to report each, right? Now I could easily use bitfields to return more error information, but, as of now though, all my functions don't have such complex error possibilities...

But, do you guys think it would be good to prepare my code(mainly, making the errors multiples of 2, so they can represent individual bits), so that if one day, I might need to return complex errors, it wouldn't be too hard to change the code?

Thanx...
{2}rIng
Report
Re: Error reporting design for....C... Posted by Lundin on 5 Mar 2007 at 6:27 AM
: Sorry, no exceptions and throws and catches here...
:
: Since my program is in C, I can't use fancy C++ features to report errors to callers of the functions I use, so instead, I decided on a common return value set. Each function returns one value from this set:
:
:
: #define SINT_SUCCESS        0/*returned on successful operation*/
: #define SINT_ERR_ARG        1/*one or more of the arguments passed are faulty*/
: #define SINT_ERR_DYNMEM     2/*there was an error in dynamic allocation*/
: #define SINT_ERR_TIDY       3/*removal of excess bytes in the output failed. This is not fatal*/
: #define SINT_ERR_IO_BADBASE 4/*the base passed to an IO function is not supported*/
: ...
: 

:
:
: Now there is a weakness in this system. Only one error can be reported for each call. Lets say we have some number of errors, we need to be able to report each, right? Now I could easily use bitfields to return more error information, but, as of now though, all my functions don't have such complex error possibilities...
:
: But, do you guys think it would be good to prepare my code(mainly, making the errors multiples of 2, so they can represent individual bits), so that if one day, I might need to return complex errors, it wouldn't be too hard to change the code?
:
: Thanx...
: {2}rIng
:


The common way to do this in C is like this:

typedef enum
{
  SINT_SUCCESS        = 0x00,
  SINT_ERR_ARG        = 0x01,
  SINT_ERR_DYNMEM     = 0x02,
  SINT_ERR_TIDY       = 0x04,
  SINT_ERR_IO_BADBASE = 0x08,
  SINT_ANOTHER_ERROR  = 0x10,
  ...

} ErrorType;


ErrorType myErrorProneFunction (void);


int main()
{
  ErrorType result;
  result = myErrorProneFunction();

  if(result != SINT_SUCCESS)
  {
    if(result & SINT_ERR_ARG)
    { 
      /* error handling here */
    }
  }

  return 0;
}






 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.