G
Gregory
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
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