473,703 Members | 3,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reference-counted error objects

Hi,

One of the disadvantages of using error handling with error codes
instead of exception handling is that error codes retuned from a
function can be forgotten to check thus leading to
unexpected run-time problems. Exceptions on the other hand can not be
simply ignored.
In order to solve this error codes problem I suggest using error object
instead of error codes.
Each function has to return an error object that stores an error
condition (true/false) that must be eventually tested, otherwise
warning/exit/exception (may be not a good idea) will be generated.
By eventually I mean that it must not be the same instance of the error
object that has to be tested, it can also be one of its saved copies
(copy-constructed or assigned w/ operator=) that are passed along the
program flow.

Such error object may be implemented using internal reference counter
which tracks all its copies and if none of the copies was tested the
last copy destructor signalizes the problem.

class Error
{
public:
Error(bool hasErr, const string& file, int line);

~Error()
{
if (! m_refCntError->m_wasChecked )
{
cerr << "WARNING: Error at file " <<
m_refCntError->m_file << ", line " <<
m_refCntError->m_line <<
" has been never checked !" << endl;
}
}

bool hasError()
{
// Public member for simplicity
m_refCntError->m_wasChecked = true;

return m_refCntError->hasError();
}

// Reference-counted implementation
Error(const Error&);
Error& operator=(const Error&);
.......
private:
RefCntError *m_refCntError;
};

If none of the error object copies called hasError() method the warning
is issued.

There is obvious performance penalty using error objects as opposed to
the error codes.
So it can be only used in the debug mode.

Does this strategy make sense ?

Gregory

Nov 9 '05 #1
2 2971
Gregory wrote:
Hi,

One of the disadvantages of using error handling with error codes
instead of exception handling is that error codes retuned from a
function can be forgotten to check thus leading to
unexpected run-time problems. Exceptions on the other hand can not be
simply ignored.
In order to solve this error codes problem I suggest using error object
instead of error codes.
Each function has to return an error object that stores an error
condition (true/false) that must be eventually tested, otherwise
warning/exit/exception (may be not a good idea) will be generated.
By eventually I mean that it must not be the same instance of the error
object that has to be tested, it can also be one of its saved copies
(copy-constructed or assigned w/ operator=) that are passed along the
program flow.

Such error object may be implemented using internal reference counter
which tracks all its copies and if none of the copies was tested the
last copy destructor signalizes the problem.

class Error
{
public:
Error(bool hasErr, const string& file, int line);

~Error()
{
if (! m_refCntError->m_wasChecked )
{
cerr << "WARNING: Error at file " <<
m_refCntError->m_file << ", line " <<
m_refCntError->m_line <<
" has been never checked !" << endl;
}
}

bool hasError()
{
// Public member for simplicity
m_refCntError->m_wasChecked = true;

return m_refCntError->hasError();
}

// Reference-counted implementation
Error(const Error&);
Error& operator=(const Error&);
.......
private:
RefCntError *m_refCntError;
};

If none of the error object copies called hasError() method the warning
is issued.

There is obvious performance penalty using error objects as opposed to
the error codes.
So it can be only used in the debug mode.

Does this strategy make sense ?


Yes, a class to enforce error checking does make sense, particularly if
the program has some reason not to adopt exceptions for error handling.

I do have a few suggestions for simplifying and streamlining the Error
class:

For reasons of efficiency, the Error class should not be any larger
than the error code itself. If it were the same size as an error code,
then Error objects could be passed and returned by value fairly
efficiently.

Along these lines, it is not really necessary to ref count Error
instances. A simpler approach I think would be to have each error
result have only one Error object "owner" at a time. An Error object
that owns a result must either be checked or have its error result
copied to another Error object, before it is destroyed. In a way, an
error result something of a "hot potato", the routine that receives it
must either deal with it or pass it up the calling chain. But
eventually some code somewhere will have to check it.

Lastly, there should be as little difference as possible in how errors
are handled between the debug and production builds. After all, the
software that is shipped should be as close as possible to the software
that was tested. A more efficient Error class could be used in a
non-debug builds, with the only difference being that asserts become
no-ops.

To illustrate how these suggestions might be implemented, I have
written an Error class (below). An Error object is the same size as an
error code (an int in this example). An Error object, once assigned an
error result, becomes the "owner" of that result. The program must
ensure that an error result is either read or copied to another Error
object before the Error object owner is destroyed. A few test cases are
provided to give a better idea of how this all works.

#include <assert.h>

class Error
{
mutable int mErrorCode;

// the value of kErrorCodeCheck ed should not conflict
// with any actual error code

static const int kErrorCodeCheck ed = -1;

void ClearError() const
{
mErrorCode = kErrorCodeCheck ed;
}

public:
Error( int err = kErrorCodeCheck ed)
: mErrorCode( err)
{
}

Error( const Error& rhs)
: mErrorCode( rhs.GetErrorCod e())
{
}

~Error()
{
assert( mErrorCode == kErrorCodeCheck ed);
}

int GetErrorCode() const
{
int err = mErrorCode;
ClearError();
return err;
}

Error& operator=(int err)
{
assert(mErrorCo de == kErrorCodeCheck ed);

mErrorCode = err;
return *this;
}

Error& operator=(const Error& rhs)
{
assert(mErrorCo de == kErrorCodeCheck ed);

mErrorCode = rhs.GetErrorCod e();
return *this;
}
};
// Test Cases

// a routine that returns an error
Error ReturnsError()
{
int a = 5 * rand();

return -10;
}

// Fails - Error code discarded
Error
TestFailureOne( )
{
ReturnsError();
return 0;
}

// Fails - Error code stored but not retrieved
Error
TestFailureTwo( )
{
Error err = ReturnsError();

return 0;
}

// OK - Error code is checked
Error
TestCaseOne()
{
Error err = ReturnsError();
int err = err.GetErrorCod e();

return 0;
}

// OK - Error code is returned to caller
Error
TestCaseTwo()
{
Error err = ReturnsError();

return err;
}

int main()
{
TestFailureOne( );
TestFailureTwo( );
TestCaseOne();
TestCaseTwo();
}

Greg

Nov 10 '05 #2
Greg, thanks ! You comments are very insightful. I like
your "hot potato" metaphore :) Indeed I do not see any need in the
tracking of
all error object copies all together (using ref-conting). At least I
can't find an
example justifying these efforts. Yes, "single ownership" is a proper
strategy.
However it seems better to separate mErrorCode two responsibilitie s of
storing error
code and indicating never-checked error.
Here is an example of such necessity:

Error Test()
{
Error err = ReturnsError();

if (err.GetErrorCo de())
{
// err already does not contain original error code
return err;
}

return 0;
}

Returned 'err' already erased its original error code. If we separated
the above mentioned responsibities, then the error code check flag
would be marked and the error code
would be left unchanged. We could add additional mErrorCheck private
data memeber to the Error class and treat it and mErrorCode separately.

By the way all four test functions in your main() will be asserted
right after they return (returned error is not checked).

Another issue is in the following example:

Error test()
{
Error err1 = ReturnsError();
Error err2 = ReturnsError();
Error err3 = ReturnsError();

if (err1.GetErrorC ode() || err2.GetErrorCo de() ||
err3.GetErrorCo de())
{
return -1; // return error
}
}

Using lazy evaluation of OR statement neither the second nor the third
error might
will be checked. My code also suffers from this problem.
May be it's just bad style to leave error unchecked for some time, may
be not. But I don't
se appropriate solution for this problem and this code is sometimes
more clear and easy to write.

Regards,

Gregory

Nov 10 '05 #3

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

Similar topics

9
2997
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody tell me which one will be more efficient and why? as far as i know both must be same, because i read somewhere that internally
11
2193
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
4
3407
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any typos I am a newbie here ;-)
22
3493
by: tshad | last post by:
If I am passing a variable by reference to another routine by reference, do I need to dereference first? string testString; .... FirstSub(ref firstString) { HandleString(ref firstString); //do I need to de reference here or just pass theString without the "ref"
3
789
by: Michael Sgier | last post by:
Hi i get thousands of messages like below. How shall i resolve that? Thanks Mcihael Release/src/Utility/RawImage.o: In function `CMaskImage::CMaskImage(int, int, char const*)': RawImage.cpp:(.text+0x15dc): undefined reference to `popen(PFS*, char const*)'
9
1889
by: Edward Diener | last post by:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference to a reference in C#. I know one can use it on a value type to create a reference to that value.
10
13656
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the function is also modified. Sometimes I wish to work with "copies", in that when I pass in an integer variable into a function, I want the function to be modifying a COPY, not the reference. Is this possible?
41
3650
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what pointer and reference are and how they relate to each other.
68
4616
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a temporary but it was stated that it would not. This has come up in an irc channel but I can not find the original thread, nor can I get any code to work. Foo& Bar( int Val ) { return Foo( Val ); }
275
12305
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
8759
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 usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9017
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8963
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4433
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2453
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2069
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.