D
DaKoadMunky
Say I have the following...
class Foo
{
public:
Foo() : bar(0) {}
void SetBar(int);
private:
int bar;
};
Say that an invariant for my class is that Foo::bar always be an even number.
If somebody calls Foo::SetBar(int) with an odd number what is the best way to
handle it?
1) assertions while in debug mode (if release mode let caller suffer the
consequences)?
2) throw an exception?
3) return an error code?
4) other?
In all cases I would leave the value of Foo::bar unchanged. The question is
how and if to let the caller know the value was not changed.
Throwing an exception seems like the least likely solution. Is it valid to use
exceptions to invalidate input or is that an abuse of exception handling?
I am leaning towards solution #1.
Any help would be appreciated.
Brian
class Foo
{
public:
Foo() : bar(0) {}
void SetBar(int);
private:
int bar;
};
Say that an invariant for my class is that Foo::bar always be an even number.
If somebody calls Foo::SetBar(int) with an odd number what is the best way to
handle it?
1) assertions while in debug mode (if release mode let caller suffer the
consequences)?
2) throw an exception?
3) return an error code?
4) other?
In all cases I would leave the value of Foo::bar unchanged. The question is
how and if to let the caller know the value was not changed.
Throwing an exception seems like the least likely solution. Is it valid to use
exceptions to invalidate input or is that an abuse of exception handling?
I am leaning towards solution #1.
Any help would be appreciated.
Brian