473,226 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

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 2911
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 kErrorCodeChecked should not conflict
// with any actual error code

static const int kErrorCodeChecked = -1;

void ClearError() const
{
mErrorCode = kErrorCodeChecked;
}

public:
Error( int err = kErrorCodeChecked)
: mErrorCode( err)
{
}

Error( const Error& rhs)
: mErrorCode( rhs.GetErrorCode())
{
}

~Error()
{
assert( mErrorCode == kErrorCodeChecked);
}

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

Error& operator=(int err)
{
assert(mErrorCode == kErrorCodeChecked);

mErrorCode = err;
return *this;
}

Error& operator=(const Error& rhs)
{
assert(mErrorCode == kErrorCodeChecked);

mErrorCode = rhs.GetErrorCode();
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.GetErrorCode();

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 responsibilities of
storing error
code and indicating never-checked error.
Here is an example of such necessity:

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

if (err.GetErrorCode())
{
// 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.GetErrorCode() || err2.GetErrorCode() ||
err3.GetErrorCode())
{
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
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...
11
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
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...
22
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); ...
3
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*)':...
9
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
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...
41
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...
68
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...
275
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'
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

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.