473,407 Members | 2,314 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,407 software developers and data experts.

reporting programming error

Greetings,

Is there a consensus as to the best way to report a programming error, for
example an out of bounds index value passed to a function? There are three
approaches that I can think of:

[1] (traditional) error return value
[2] error value in object
[2] exception

Below are examples of how they might be used by functions that check their
index parameter for for out of bounds.

// [1]
er = object.process_index(index);
assert(er == 0);

// [2]
object.process_index(index);
assert(object.error == 0);

// [3]
try { object.process_index(index); }
catch(...) { assert(false); }

Programming errors are, I assume, most likely found via asserts. If this is
the case, the cleanest approach seems to be approach [2]. This prevents
cluttering up the call with error types and limits those references to the
assert.

Thanks
Jul 22 '05 #1
4 1684
Ian Lazarus wrote:
Greetings,

Is there a consensus as to the best way to report a programming error, for
example an out of bounds index value passed to a function? There are three
approaches that I can think of:

[1] (traditional) error return value
[2] error value in object
[2] exception


I for one like exceptions in this case. If the caller of my function
can't even use it as directed, I certainly don't trust said caller to
check for an error value. I'd just as soon pass error information back
as far as necessary to find a more responsible layer of the program.
Jul 22 '05 #2
Ian Lazarus wrote:
Is there a consensus as to the best way to report a programming error?
For example an out of bounds index value passed to a function?
There are three approaches that I can think of:

[1] (traditional) error return value
[2] error value in object
[3] exception

Below are examples of how they might be used by functions
that check their index parameter for for out of bounds.

// [1]
er = object.process_index(index);
assert(er == 0);

// [2]
object.process_index(index);
assert(object.error == 0);

// [3]
try { object.process_index(index); }
catch(...) { assert(false); }

Programming errors are, I assume, most likely found via asserts.
If this is the case, the cleanest approach seems to be approach [2].
This prevents cluttering up the call with error types
and limits those references to the assert.


#include <iostream>

class myClass {
private:
int max_index;
public:
myClass& process_index(int index, const char* file, int line);
myClass& process_index(int index);
};

myClass& myClass::process_index(int index,
const char* file, int line) {
if (index < 0 || max_index < index)
std::cerr << "Index out of bounds "
<< "in function myClass::process_index(int) "
<< "at line #" << line
<< " in file " << file << std::endl;
return process_index(index);
}

#ifndef NDEBUG
#define process_index(index) \
process_index((index), __FILE__, __LINE__)
#endif//NDEBUG

Jul 22 '05 #3
"Ian Lazarus" <no****@nowhere.net> wrote
Is there a consensus as to the best way to report a programming
error, for example an out of bounds index value passed to a
function?
There's definitely no consensus on the best way, but there is consensus on
various wrong ways.
There are three approaches that I can think of:

[1] (traditional) error return value
[2] error value in object
[2] exception

Below are examples of how they might be used by functions that check their
index parameter for for out of bounds.

// [1]
er = object.process_index(index);
assert(er == 0);
This is the old-style C way of handling errors. The advantages are that it's
simple and it allows a programmer to ignore the error. The disadvantage is that
it allows the programmer to ignore the error.
// [2]
object.process_index(index);
assert(object.error == 0);
Except under rare conditions, this is an all-around bad approach. The exception
applies to those objects whose internal state actually does change due to an
error, such as streams, sockets, files, etc. and where the object can no longer
be used until the condition is rectified. In practically all other cases, it's a
design error that associates the exit state of the operation with the static
state of the object. The only place one should be interested in the last error is
where the error occurred. Let's not even get into the mess that this approach can
cause in multi-threaded programs.
// [3]
try { object.process_index(index); }
catch(...) { assert(false); }
This is the "standard" C++ way of handling errors. However, it has one major
disadvantage: the caller can't ignore the error. Of course, the advantage is that
the caller can't ignore the error. :-)
Programming errors are, I assume, most likely found via asserts.
The case for 'assert' is fuzzier. Many people object to asserts on a variety of
grounds, the most often voiced being that the non-debug code will be different
from the debug version and any inadvertent side-effects introduced in the asserts
will be missing from the non-debug code. Others feel that better error-handling
(standardized logging, etc.) should be used instead. Personally, I like liberally
sprinkling asserts in my code, but you shouldn't assume that it's a universal
sentiment.
If this is the case, the cleanest approach seems to be approach [2].
This prevents cluttering up the call with error types and limits those
references to the assert.


while (true) {
std::cerr << "NO!!!" << std::endl;
}

[1] and [3] are acceptable (for the record, I prefer [3]). [2] is simply
unacceptable unless the error really is part of the object's natural state.

Claudio Puviani
Jul 22 '05 #4
> Programming errors are, I assume, most likely found via asserts. If this is
the case, the cleanest approach seems to be approach [2]. This prevents
cluttering up the call with error types and limits those references to the
assert.


If your software is interactive and it's difficult to run it
twice with the same input (word processor, gui, ...), or if
program execution should never stop (monitoring system,
....), I think that the programmer should assume that there
are bugs, i.e. not rely on assert, handle errors in a clean
way (I don't like to loose data because the application
crashed due to an internal error) and output as much
information as possible to enable debugging without running
the program again because you might be unable to reproduce
the problem. The program should behave as if that particular
feature where the bug happened is missing.
Jul 22 '05 #5

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

Similar topics

4
by: Fabian von Romberg | last post by:
Hi, I have installed Sql Reporting Services on 2 machines, one is WIN 2000 PRO and the other one is WIN 2000 ADV. SERVER. When I try to access a report using the webbrowser, I get the following...
0
by: kt | last post by:
We want to automatically turn off the error reporting options using Windows API functions for the following functions area Click on Start, Settings, Control Panel, System, the Advanced tab and...
0
by: Joshua V. | last post by:
We are trying to implement Windows Error Reporting from within our VB.Net application. Basically we want to replace the standard unhandled exception message we use with the Windows Error Reporting...
5
by: Paul Furman | last post by:
What is the proper way to turn off error reporting? I'm on a shared server so... "Note: Although display_errors may be set at runtime (with ini_set ()), it won't have any affect if the script...
1
by: rmk | last post by:
How can I get the 2000 and 2005 versions of SQL Server Reporting Services both working on my development laptop ????? I have ASP.NET 1.1 and 2.0 installed on my laptop. I have Visual Studio...
1
by: Melissa Nava | last post by:
Have an online system. Currently the error reporting either does: A) logs to a file, B) databases errors or C) emails individual errors. Also have phone notifications for systematically calling the...
3
by: =?Utf-8?B?QS4gUm9iaW5zb24=?= | last post by:
I have been looking high and low and can't seem to find anything a little more detailed on the whole RS utility. What I'm looking for specifically is some type of programming reference. Also, I...
8
by: Paul Furman | last post by:
How do I turn off MySQL error reporting? I set error_reporting(0); but that doesn't seem to be working.
0
by: fperri | last post by:
Hi, I am trying to configure reporting services which I just added to an already existing installation of SQL Server 2005. When I am in the reporting services configuration manager and I am trying...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.