|
|
Orchid XP v8 <voi### [at] devnull> wrote:
> What is the "usual" C++ style for this kind of thing? (I'm asking a
> question of style here; clearly there are several possible things you
> *could* do in C++.)
The standard data containers, as well as 'new', throw an exception by
default when they run out of memory. Also the at() member function of
some of the containers throws an exception if you try to index out of
boundaries.
If you don't want to throw exceptions in your own code, then it depends
on what is it that you want to do to signal a condition.
The usual way to catch programming errors at runtime is to use
assertions. For example:
void foo(int min, int max)
{
assert(min <= max);
// rest of the code here
}
If the assertion fails, a message will be printed and the program is
immediately terminated at that point. Obviously this should only be
used in situations which are a symptom of a programming error and which
must be fixed.
(The good thing about assert() is that you can freely use it as much
as you want. In fact, the more you use it, the better. You can compile
the program with a special precompiler parameter which will remove all
the asserts from the executable so that they will not add overhead to
a final version.)
--
- Warp
Post a reply to this message
|
|