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

::std sometimes needed, sometimes not

Hi there. How come when in one file i unclude <cmath> i need to use
std::tan(...), and in another file i include <cassert> and std::assert fails
to compile. Nowhere in my code i do using namespace std; Seams like some
things in my header files are in std namespace and some are not. Is that
correct, or is my compiler (mingw, 3.2 i think) horribly broken? :o)

Thanx,
Martin


Jul 19 '05 #1
24 1872
On Mon, 15 Sep 2003 20:41:01 -0400, "Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote:
Hi there. How come when in one file i unclude <cmath> i need to use
std::tan(...), and in another file i include <cassert> and std::assert fails
to compile.
That's because assert is a macro. It doesn't live in a namespace; no
macro does. That's one problem with macros; the nice thing about them
is that they can pick up things like line number and so on.

Nowhere in my code i do using namespace std; Seams like some
things in my header files are in std namespace and some are not. Is that
correct
Yes, but there are only a handful or so of standard macros.

or is my compiler (mingw, 3.2 i think) horribly broken? :o)


Wrt. to some other things, yes, but it's still a very good compiler.

Jul 19 '05 #2
OK, so is there a "C++" way of doing assert? A standard C++ class/template
or something that does assert? Also, is there a C++ alternative to <cmath>?

Thanx,
Martin
"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On Mon, 15 Sep 2003 20:41:01 -0400, "Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote:
Hi there. How come when in one file i unclude <cmath> i need to use
std::tan(...), and in another file i include <cassert> and std::assert failsto compile.


That's because assert is a macro. It doesn't live in a namespace; no
macro does. That's one problem with macros; the nice thing about them
is that they can pick up things like line number and so on.

Nowhere in my code i do using namespace std; Seams like some
things in my header files are in std namespace and some are not. Is that
correct


Yes, but there are only a handful or so of standard macros.

or is my compiler (mingw, 3.2 i think) horribly broken? :o)


Wrt. to some other things, yes, but it's still a very good compiler.

Jul 19 '05 #3
Marcin Vorbrodt wrote:
OK, so is there a "C++" way of doing assert? A standard C++ class/template
or something that does assert? Also, is there a C++ alternative to <cmath>?


Please don't top-post here. Read section 5 of the FAQ for posting
guidelines:

http://www.parashift.com/c++-faq-lite/

C++ doesn't have anything assert-like built in (other than assert), but
you could certainly create something, probably using an exception class
to carry the information about the error. Stroustrup has an example of
this somewhere, IIRC. Probably in "The C++ Programming Language".

<cmath> *is* the C++ alternative.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4
Kevin Goodsell <us*********************@neverbox.com> writes:
Marcin Vorbrodt wrote:
OK, so is there a "C++" way of doing assert? A standard C++ class/template
or something that does assert? Also, is there a C++ alternative to <cmath>?

Please don't top-post here. Read section 5 of the FAQ for posting
guidelines:

http://www.parashift.com/c++-faq-lite/

C++ doesn't have anything assert-like built in (other than assert),
but you could certainly create something, probably using an exception
class to carry the information about the error.


Before you write code signaling a programmer error with an exception,
read this thread on assert versus exceptions:

http://groups.google.com/gr*********...ing.google.com
or:
http://tinyurl.com/nhp7
Stroustrup has an
example of this somewhere, IIRC. Probably in "The C++ Programming
Language".

[snip]

He does. However, other experts have since argued that is generally
bad advice.

Jul 19 '05 #5
llewelly wrote:


Before you write code signaling a programmer error with an exception,
read this thread on assert versus exceptions:

http://groups.google.com/gr*********...ing.google.com
or:
http://tinyurl.com/nhp7
I don't know if you were talking specifically about the particular
message that link goes to, but I don't think I could disagree more with
this point:

- You definitely do NOT want to throw an exception in case of a
contract violation. It is very, very rare, if ever, that this is
correct. The program is incorrect, and you abort.

The fact that every function can potentially throw an exception
makes writing exception safe code pratically impossible.

A lot of people seem to want exceptions anyway:-). To keep them
happy, I would suggest some sort of callback, which the user can
replace. By default, the code should abort. If the user replaces
it with something which will throw, his code probably will not work
when the exception occurs. But since that's what he wanted...
This sounds like someone who doesn't understand exceptions to me.

Stroustrup has an
example of this somewhere, IIRC. Probably in "The C++ Programming
Language".


[snip]

He does. However, other experts have since argued that is generally
bad advice.


Reference? Without reading the opposing argument, I'm very likely to
side with Stroustrup over some generic, unnamed "experts".

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #6
Kevin Goodsell wrote:
llewelly wrote:
...

> - You definitely do NOT want to throw an exception in case of a
contract violation. It is very, very rare, if ever, that this is
correct. The program is incorrect, and you abort.

The fact that every function can potentially throw an exception
makes writing exception safe code pratically impossible.

A lot of people seem to want exceptions anyway:-). To keep them
happy, I would suggest some sort of callback, which the user can
replace. By default, the code should abort. If the user replaces
it with something which will throw, his code probably will not work
when the exception occurs. But since that's what he wanted...

This sounds like someone who doesn't understand exceptions to me.


OK - time for some friendly debate.

I'll state that I use exceptions very rarely and hardly ever in
production code.

I actually agree the the quote 100% because I have done exactly that and
found that it is a very acceptable paradigm for developing solid and
easily maintainable code.

I have found that exceptions leed to sloppy code by less experienced
programmers and even some more experienced guys as well.

However, I'm open to suggestions. I'm only 90% conviced they're next to
useless. OK OK OK. In some rare circumstances I can see that they are
an acceptable alternative. In a test framework, I designed, throwing an
exception was a way to indicate a test failure, abort()ing was
unsatisfactory since it meant that other tests would have not been able
to run in that particular framework.

Jul 19 '05 #7
>>
- You definitely do NOT want to throw an exception in case of a
contract violation. It is very, very rare, if ever, that this is
correct. The program is incorrect, and you abort.

The fact that every function can potentially throw an exception
makes writing exception safe code pratically impossible.

A lot of people seem to want exceptions anyway:-). To keep them
happy, I would suggest some sort of callback, which the user can
replace. By default, the code should abort. If the user replaces
it with something which will throw, his code probably will not work
when the exception occurs. But since that's what he wanted...

This sounds like someone who doesn't understand exceptions to me.
However, I'm open to suggestions. I'm only 90% conviced they're next to
useless. OK OK OK. In some rare circumstances I can see that they are
an acceptable alternative. In a test framework, I designed, throwing an
exception was a way to indicate a test failure, abort()ing was
unsatisfactory since it meant that other tests would have not been able
to run in that particular framework.


I use exceptions primarily for signaling constructor failure, since it
is the only way. Do you have a workaround or you assume your ctors
always succeed ? :)

Jonathan

Jul 19 '05 #8
Jonathan Mcdougall wrote:
>>> - You definitely do NOT want to throw an exception in case of a
contract violation. It is very, very rare, if ever, that this is
correct. The program is incorrect, and you abort.

The fact that every function can potentially throw an exception
makes writing exception safe code pratically impossible.

A lot of people seem to want exceptions anyway:-). To keep them
happy, I would suggest some sort of callback, which the user can
replace. By default, the code should abort. If the user replaces
it with something which will throw, his code probably will not work
when the exception occurs. But since that's what he wanted...

This sounds like someone who doesn't understand exceptions to me.


However, I'm open to suggestions. I'm only 90% conviced they're next to
useless. OK OK OK. In some rare circumstances I can see that they are
an acceptable alternative. In a test framework, I designed, throwing an
exception was a way to indicate a test failure, abort()ing was
unsatisfactory since it meant that other tests would have not been able
to run in that particular framework.

I use exceptions primarily for signaling constructor failure, since it
is the only way. Do you have a workaround or you assume your ctors
always succeed ? :)


Very few contructors I create fail. bad_alloc is a given but I usually
have bigger problems than bad_alloc exceptions. (I think I have yet to
see a bad_alloc exception thrown in a production environment) With most
systems now allowing you to overcommit on memory, you're more likely to
get an access violation/segmentation fault when memory is depleted.

I'll give you that one, but I think it's rare and hence letting it abort
may be a suitable alternative most of the time.

Anything else ?

Jul 19 '05 #9
>>>However, I'm open to suggestions. I'm only 90% conviced they're next to
useless. OK OK OK. In some rare circumstances I can see that they are
an acceptable alternative. In a test framework, I designed, throwing an
exception was a way to indicate a test failure, abort()ing was
unsatisfactory since it meant that other tests would have not been able
to run in that particular framework.

I use exceptions primarily for signaling constructor failure, since it
is the only way. Do you have a workaround or you assume your ctors
always succeed ? :)


Very few contructors I create fail.


That's what I thought :)
bad_alloc is a given but I usually
have bigger problems than bad_alloc exceptions.
And you usually abort on these problems?
(I think I have yet to
see a bad_alloc exception thrown in a production environment)
Happens often when you try to run recent software on old machines,
believe me :)
With most
systems now allowing you to overcommit on memory, you're more likely to
get an access violation/segmentation fault when memory is depleted.
Mmm.. I didn't know that.
I'll give you that one, but I think it's rare and hence letting it abort
may be a suitable alternative most of the time.


I think aborting is never a suitable alternative. Letting a catchable
exception propagate is imho bad programming. Trying something else
(or the same thing), warning the user, asking to save.. you know..
user-friendly programming. Nothing frustrates me more than a program
terminating without warning.

I long believed my programs would never fail as others do, but now I
try to do "preventive programming".

I understand there are problems you just cannot foresee or even solve
during runtime, but I try to do as much as possible to prevent my
applications from crashing.
Jonathan

Jul 19 '05 #10
Jonathan Mcdougall wrote:
....
And you usually abort on these problems?
Yep.
(I think I have yet to
see a bad_alloc exception thrown in a production environment)

Happens often when you try to run recent software on old machines,
believe me :)


Hmm. I have developed software that ran on thousands of machines and
other problems were reported, but out of memory errors were not among them.


With most
systems now allowing you to overcommit on memory, you're more likely to
get an access violation/segmentation fault when memory is depleted.

Mmm.. I didn't know that.


I may have overstated that a bit, but I have seen this.

I'll give you that one, but I think it's rare and hence letting it abort
may be a suitable alternative most of the time.

I think aborting is never a suitable alternative. Letting a catchable
exception propagate is imho bad programming.

On some systems (Unix) you get a nice big fat core file pointing you to
the code and the reason for the fault. Sounds like a wonderful
alternative to me !

.... Trying something else (or the same thing), warning the user, asking to save.. you know..
user-friendly programming. Nothing frustrates me more than a program
terminating without warning.
Sometimes there are more important features. If you can solve the
problem with a memory upgrade, it's often cheaper than complicating your
code.

I long believed my programs would never fail as others do, but now I
try to do "preventive programming".

I understand there are problems you just cannot foresee or even solve
during runtime, but I try to do as much as possible to prevent my
applications from crashing.


Not letting them happen in the first place is the key.

There are ways to guarentee that you can't get a bad_alloc but it
requires knowing before-hand what you're going to need - essentially
pre-allocating the memory.

Seems like alot of trouble for not alot of gain.

If I'm not mistaken, it sounds like we may have a different bias but we
agree on the problem.


Jul 19 '05 #11
Gianni Mariani wrote:
Kevin Goodsell wrote:

>> - You definitely do NOT want to throw an exception in case of a
contract violation. It is very, very rare, if ever, that this is
correct. The program is incorrect, and you abort.

The fact that every function can potentially throw an exception
makes writing exception safe code pratically impossible.

A lot of people seem to want exceptions anyway:-). To keep them
happy, I would suggest some sort of callback, which the user can
replace. By default, the code should abort. If the user replaces
it with something which will throw, his code probably will not work
when the exception occurs. But since that's what he wanted...

This sounds like someone who doesn't understand exceptions to me.

OK - time for some friendly debate.

I'll state that I use exceptions very rarely and hardly ever in
production code.

I actually agree the the quote 100% because I have done exactly that and
found that it is a very acceptable paradigm for developing solid and
easily maintainable code.


I don't think that library code should ever unilaterally abort the
program. Besides that, doing so causes objects to go undestructed,
leading to resource leaks (sure, most modern desktop systems clean up
when the process terminates, but you can't count on that in portable code).

I don't understand why you would prefer aborting to throwing an
exception anyway. What's the worst case for throwing an exception? It's
not caught, and the program aborts. So by aborting instead of throwing,
you're ensuring that you always get the worst case.

I have found that exceptions leed to sloppy code by less experienced
programmers and even some more experienced guys as well.

However, I'm open to suggestions. I'm only 90% conviced they're next to
useless. OK OK OK. In some rare circumstances I can see that they are
an acceptable alternative. In a test framework, I designed, throwing an
exception was a way to indicate a test failure, abort()ing was
unsatisfactory since it meant that other tests would have not been able
to run in that particular framework.


Well, I have to admit that I don't have a lot of experience actually
using exception handling, but I like it a lot. I find that it makes code
considerably easier to write, and less cluttered. During development of
a library, I just catch everything in main and print the what() string.
That way when I run a quick test I get a nice little report of what went
wrong (if anything). In a program using the library I just catch
wherever is most appropriate.

I don't know what else to even say about it. It makes things so simple I
can't imagine why you wouldn't want to use it. I hate having to
constantly check return values for errors. Speaking of sloppy
programming, how often do people forget to check return values? It's a
bit harder to ignore an exception, and easier to track down the
resulting problem if you do. Ignore a return value and your program may
limp along in an undefined state for a while before crashing in some
other part of the program. With an uncaught exception it's immediate,
and the debugger can show you where it was thrown from.

I might have to ask you to explain to me what's so bad about exceptions. :-/

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #12
In article <jr****************@newsread3.news.pas.earthlink.n et>,
us*********************@neverbox.com says...

[ commenting on a quote from James Kanze ... ]
This sounds like someone who doesn't understand exceptions to me.
Others would disagree. Just for example, Herb Sutter (author of
Exceptional C++, More Exceptional C++ and an upcoming and TTBOMK, as yet
unnamed book on exceptions) lists James Kanze as "guru" here:

http://www.gotw.ca/gotw/056.htm

[ ... ]
Reference? Without reading the opposing argument, I'm very likely to
side with Stroustrup over some generic, unnamed "experts".


I'm not sure of the arguments in question either, but if James Kanze is
among those making the argument, I'd guess that at the very least, it's
worth studying in detail. Herb Sutter isn't the only one who names or
acknowledges him either -- he is, for example, listed in the
acknowledgements of a number of books I have handy, including (for one
example) _C++ In a Nutshell_, from O'Reilly and Associates (who
generally aren't disposed toward publishing really bad books anyway).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #13
In article <Cq****************@newsread3.news.pas.earthlink.n et>,
us*********************@neverbox.com says...

[ ... ]
I don't understand why you would prefer aborting to throwing an
exception anyway. What's the worst case for throwing an exception? It's
not caught, and the program aborts. So by aborting instead of throwing,
you're ensuring that you always get the worst case.


A contrast violation means there is something wrong with the code (i.e.
a bug) that has been found and noted, and needs to be fixed.

In this case, aborting is a LONG ways from being the worst case. Quite
the contrary: the worst case is that the problem is allowed to continue
unnoticed for some arbitrary period of time -- almost inevitably, the
longer a bug persists, the more difficult and expensive it is to fix.
Therefore, when you DO find what you're sure is a bug in some code, the
best case is to ensure that it fails as quickly, dependably and
spectacularly as possible, so it will be fixed as soon as possible
instead of being allowed to persist.

The problem with throwing an exception in a case like this is that it's
all too easy (and common) for somebody to do something like this:

int main(int argc, char **argv) {
do {
try {
return real_main(argc, char **argv);
}
catch(...) {
std::cerr<<"Some problem -- retrying\n";
}
} while(1);
return 0; // never really executes
}

In fact, almost any instance of "catch(...)" can cover up an almost
arbitrary collection of problems that the library designer is TRYING to
make sure get noticed.

One way to avoid that is to abort instead of throwing an exception -- as
long as you're sure that what you've got is a bug in the code, and not
something from which recovery is reasonable, abort() is a better option
precisely because it prevents any other part of the code from covering
up the problem.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #14
Jerry Coffin wrote:
In article <jr****************@newsread3.news.pas.earthlink.n et>,
us*********************@neverbox.com says...

[ commenting on a quote from James Kanze ... ]
This sounds like someone who doesn't understand exceptions to me.


Others would disagree. Just for example, Herb Sutter (author of
Exceptional C++, More Exceptional C++ and an upcoming and TTBOMK, as
yet unnamed book on exceptions) lists James Kanze as "guru" here:


James is a guru. A practical one. So some of his advice is not about how
to do things in a perfect world where all compilers are conforming to the
standard, but how to do things in the real world. He is a top consultant
and rather a pragmatic, real-world guy than a language lawyer. It is worth
to listen to him.

--
Attila aka WW
Jul 19 '05 #15
Jerry Coffin wrote:
In article <jr****************@newsread3.news.pas.earthlink.n et>,
us*********************@neverbox.com says...

[ commenting on a quote from James Kanze ... ]

This sounds like someone who doesn't understand exceptions to me.

Others would disagree. Just for example, Herb Sutter (author of
Exceptional C++, More Exceptional C++ and an upcoming and TTBOMK, as yet
unnamed book on exceptions) lists James Kanze as "guru" here:

http://www.gotw.ca/gotw/056.htm

[ ... ]

Reference? Without reading the opposing argument, I'm very likely to
side with Stroustrup over some generic, unnamed "experts".

I'm not sure of the arguments in question either, but if James Kanze is
among those making the argument, I'd guess that at the very least, it's
worth studying in detail. Herb Sutter isn't the only one who names or
acknowledges him either -- he is, for example, listed in the
acknowledgements of a number of books I have handy, including (for one
example) _C++ In a Nutshell_, from O'Reilly and Associates (who
generally aren't disposed toward publishing really bad books anyway).


Heh heh. So am I, but I hope no one thinks I'm an expert.
Regards,
Buster.

Jul 19 '05 #16
Jerry Coffin wrote:

A contrast violation means there is something wrong with the code (i.e.
a bug) that has been found and noted, and needs to be fixed.
For the record, I wasn't really considering the context of the quoted
message, just exception handling in general.

Now, if you were to use exception handling, and base your exceptions off
the standard exceptions, a contract violation would be considered a
std::logic_error. There are other types of exceptions that you might use
that would not be the result of a contract violation.

But looking at just the contract violation case, obviously it's a bug
that should be fixed. But at least some bugs always make it into the
final product. Terminating the program when a problem is found is not a
very useful behavior from the perspective of the user of the
application. In fact, I don't think most users would be very happy with
that.

In this case, aborting is a LONG ways from being the worst case. Quite
the contrary: the worst case is that the problem is allowed to continue
unnoticed for some arbitrary period of time
Yes, I agree that this is bad. But it only happens if you explicitly
ignore the exception.
-- almost inevitably, the
longer a bug persists, the more difficult and expensive it is to fix.
Therefore, when you DO find what you're sure is a bug in some code, the
best case is to ensure that it fails as quickly, dependably and
spectacularly as possible, so it will be fixed as soon as possible
instead of being allowed to persist.
This is not true from the user's perspective, though.

The problem with throwing an exception in a case like this is that it's
all too easy (and common) for somebody to do something like this:

int main(int argc, char **argv) {
do {
try {
return real_main(argc, char **argv);
}
catch(...) {
std::cerr<<"Some problem -- retrying\n";
}
} while(1);
return 0; // never really executes
}

In fact, almost any instance of "catch(...)" can cover up an almost
arbitrary collection of problems that the library designer is TRYING to
make sure get noticed.


Yes, I definitely agree that this is a problem. But one way to look at
it is that it's not really *your* problem. If you threw the exception,
you've done your part. If the caller ignores it, then they screwed up.
But this doesn't make any difference in many cases. If the program
breaks for the customer, you'll still be in trouble.

So I see the point, but I don't think aborting is an ideal solution, in
particular once the program is in the customer's hands. An exception
could be an ideal solution, if it's not ignored. As far as I know,
there's no way to be sure of this.

A few ideas come to mind, but none is anywhere near perfect. You could
abort or throw depending on a build setting, for example. But if you
test with abort and ship with throw then you don't even know if the
catching code is in place or doing anything sensible. You'd have to test
with throw, and it could be covered up.

You could try to make an un-ignorable exception, maybe. Suppose your
exception object includes a 'handled' flag. On construction, this flag
is false. The catcher must set it to true. On destruction, if the flag
does not indicate that the exception was handled, the destructor aborts
the program. The problem is that it can still be ignored, it just takes
a little more effort (catch, set the flag, do nothing else).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #17

Marcin Vorbrodt <mv*****@eos.ncsu.edu> wrote in message
news:bk**********@uni00nw.unity.ncsu.edu...
Hi there. How come when in one file i unclude <cmath> i need to use
std::tan(...), and in another file i include <cassert> and std::assert fails to compile. Nowhere in my code i do using namespace std; Seams like some
things in my header files are in std namespace and some are not.
Exactly. Macros are not ('assert' is a macro) in a namespace.
They cannot be, since they're merely text substitutions.
Is that
correct, or is my compiler (mingw, 3.2 i think) horribly broken? :o)


It's fine.

-MIke

Jul 19 '05 #18
Kevin Goodsell <us*********************@neverbox.com> writes:
llewelly wrote:
Before you write code signaling a programmer error with an
exception,
read this thread on assert versus exceptions:
http://groups.google.com/gr*********...ing.google.com
or:
http://tinyurl.com/nhp7
I don't know if you were talking specifically about the particular
message that link goes to, but I don't think I could disagree more
with this point:


That's exactly the point I was referring to.

> - You definitely do NOT want to throw an exception in case of a
contract violation. It is very, very rare, if ever, that this is
correct. The program is incorrect, and you abort.

The fact that every function can potentially throw an exception
makes writing exception safe code pratically impossible.

A lot of people seem to want exceptions anyway:-). To keep them
happy, I would suggest some sort of callback, which the user can
replace. By default, the code should abort. If the user replaces
it with something which will throw, his code probably will not work
when the exception occurs. But since that's what he wanted...

This sounds like someone who doesn't understand exceptions to me.
Stroustrup has an
example of this somewhere, IIRC. Probably in "The C++ Programming
Language".

[snip]
He does. However, other experts have since argued that is generally
bad advice.


Reference? Without reading the opposing argument, I'm very likely to
side with Stroustrup over some generic, unnamed "experts".


Here's David Abrahams, with a similar opinion:
http://www.boost.org/more/error_handling.html

As for 'unnamed experts', I felt it was obvious that the name of the
expert I was referring to was James Kanze. I won't claim his
opinions should always take precedence over Stroustrup's, but
IMO, James's posts show plenty of wisdom, experience, and
insight. (With some errors here and there. :-) You should google
for his posts, and read a few to form your own opinion.

Finally, you could email Stroustrup for his current opinion on
whether exceptions should be used to signal programmer
errors. Maybe it has changed.
Jul 19 '05 #19
Gianni Mariani <gi*******@mariani.ws> writes:
Kevin Goodsell wrote:
llewelly wrote:
..
> > - You definitely do NOT want to throw an exception in case of a
contract violation. It is very, very rare, if ever, that this is
correct. The program is incorrect, and you abort.
The fact that every function can potentially throw an exception
makes writing exception safe code pratically impossible.
A lot of people seem to want exceptions anyway:-). To keep
them
happy, I would suggest some sort of callback, which the user can
replace. By default, the code should abort. If the user replaces
it with something which will throw, his code probably will not work
when the exception occurs. But since that's what he wanted...
This sounds like someone who doesn't understand exceptions to me.


OK - time for some friendly debate.

I'll state that I use exceptions very rarely and hardly ever in
production code.

I actually agree the the quote 100% because I have done exactly that
and found that it is a very acceptable paradigm for developing solid
and easily maintainable code.

I have found that exceptions leed to sloppy code by less experienced
programmers and even some more experienced guys as well.


However, I'm open to suggestions. I'm only 90% conviced they're next
to useless.

[snip]

Hm. Maybe it isn't clear from this snippet, but from what I know,
James isn't conviced exceptions are useless. He just thinks they
should be used for conditions one plans on responding to, and
*not* for programmer errors. If you read further into the
referenced thread, you'll see more.

Jul 19 '05 #20
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:tF****************@newsread3.news.pas.earthli nk.net...

Marcin Vorbrodt <mv*****@eos.ncsu.edu> wrote in message
news:bk**********@uni00nw.unity.ncsu.edu...
Hi there. How come when in one file i unclude <cmath> i need to use
std::tan(...), and in another file i include <cassert> and std::assert fails
to compile. Nowhere in my code i do using namespace std; Seams like some
things in my header files are in std namespace and some are not.


Exactly. Macros are not ('assert' is a macro) in a namespace.
They cannot be, since they're merely text substitutions.
Is that
correct, or is my compiler (mingw, 3.2 i think) horribly broken? :o)


It's fine.

-MIke


Thanks a lot! Makes sense.

Martin.

Jul 19 '05 #21
Kevin Goodsell <us*********************@neverbox.com> writes:
Gianni Mariani wrote:
Kevin Goodsell wrote:

> >> - You definitely do NOT want to throw an exception in case of a
contract violation. It is very, very rare, if ever, that this is
correct. The program is incorrect, and you abort.

The fact that every function can potentially throw an exception
makes writing exception safe code pratically impossible.

A lot of people seem to want exceptions anyway:-). To keep them
happy, I would suggest some sort of callback, which the user can
replace. By default, the code should abort. If the user replaces
it with something which will throw, his code probably will not work
when the exception occurs. But since that's what he wanted...

This sounds like someone who doesn't understand exceptions to me.

OK - time for some friendly debate.
I'll state that I use exceptions very rarely and hardly ever in
production code.
I actually agree the the quote 100% because I have done exactly that
and found that it is a very acceptable paradigm for developing solid
and easily maintainable code.


I don't think that library code should ever unilaterally abort the
program. Besides that, doing so causes objects to go undestructed,
leading to resource leaks (sure, most modern desktop systems clean up
when the process terminates, but you can't count on that in portable
code).

I don't understand why you would prefer aborting to throwing an
exception anyway. What's the worst case for throwing an exception?
It's not caught, and the program aborts. So by aborting instead of
throwing, you're ensuring that you always get the worst case.

[snip]

A contract violation occurs when a programmer has made an error
in the design or implementation of the code. So the violation
proves the code is incorrect in some unknown manner. Throwing an
exception results in the execution of cleanup code. However, the
code is known to be incorrect, but the nature of the error isn't
known precisely, so one can't know if it is safe to execute the
cleanup code; there may be some device-controlling object whose
destructor is supposed to shutdown the device, but misbehaves
terribly if the object is overwritten by e.g. memcpy with wrong
parameters. Terminating is not necessarily the worst case when
throwing an exception. Thus the abort.

There's also a conceptual mismatch. Exceptions have acompanying
features - try, catch(), destructors, - for responding to the
exception, and carrying on with the program's job. However, if
the code is wrong, the proper response is to fix the code. How do
you put that in your catch block? In general, I don't think you
can, especially not in a language like C++ .

There's an exception safety issue. Any useful level of exception
safety must rely on a few non-throwing operations. Yet in
principle a programmer error can occur in any aperation. Few
people put asserts in every function, but there are no-throw
functions complex enough to need asserts. If you try agressively
to detect programmer errors, and throw exceptions when programmer
errors are detected, I think you'll find it much more difficult,
to make your code exception safe.

Finally, there's an environment support issue. On many
implementations, throwing an exception unwinds the stack,
destroying any informantion about where the error was
detected. Typically, no stack trace is printed when the program
terminate()s due to an uncaught exception. By contrast, abort()
often leaves a core image behind, which can be loaded into a
debugger and inspected.

This isn't an argument for never using exceptions; I believe
exceptions are for conditions the code 'understands', and is
designed to repsond to. abort() is for conditions that can only
occur if some programmer has written incorrect code.

Jul 19 '05 #22
In article <bk**********@news7.svr.pol.co.uk>, bu****@none.com says...

[ ... ]
Heh heh. So am I, but I hope no one thinks I'm an expert.


No insult intended, but thinking about it a moment, that particular book
probably was a bad choice, and I apologize for mentioning it -- not
because I think it's a bad book, but the way in which I mentioned it
could (easily) be seen as self-serving, which I honestly didn't intend.

That aside, I think James Kanze is a respectable (and respected) C++
programmer with a tremendous amount of knowledge; while he's human, and
therefore may be wrong at times, dismissing his opinion without careful
consideration is usually a serious mistake, at least IMO.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #23
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<yU*****************@newsread4.news.pas.earth link.net>...
A few ideas come to mind, but none is anywhere near perfect. You could
abort or throw depending on a build setting, for example. But if you
test with abort and ship with throw then you don't even know if the
catching code is in place or doing anything sensible. You'd have to test
with throw, and it could be covered up.


That's exactly what I do and I don't see any problem. I am working on
small scale stuff though, with a build time measaured in a few minutes
at most, not several hours. It is very easy for me to do a release
build during testing to check that the catching code does what it is
supposed to (which isn't much - write the error message to a file and
exit the program).

GJD
Jul 19 '05 #24
In article <yU*****************@newsread4.news.pas.earthlink. net>,
us*********************@neverbox.com says...

[ ... ]
For the record, I wasn't really considering the context of the quoted
message, just exception handling in general.
That explains a LOT. I certainly don't think James meant that comment
in general (i.e. that you should all exceptions would be better off as
abort()'s).

[ ... ]
But looking at just the contract violation case, obviously it's a bug
that should be fixed. But at least some bugs always make it into the
final product. Terminating the program when a problem is found is not a
very useful behavior from the perspective of the user of the
application. In fact, I don't think most users would be very happy with
that.
I quite agree, and James basically allowed for the same possibility:
handle the situation with a call-back function. The default call-back
abort()'s, but the user of the class can register a different one that
does what he deems appropriate -- probably throw some exception.

[ ... ]
Yes, I agree that this is bad. But it only happens if you explicitly
ignore the exception.
Yes, but explicitly ignoring the exception (or handling it
inappropriately, if it inherits from an inappropriate base) is often
very easy to do.
-- almost inevitably, the
longer a bug persists, the more difficult and expensive it is to fix.
Therefore, when you DO find what you're sure is a bug in some code, the
best case is to ensure that it fails as quickly, dependably and
spectacularly as possible, so it will be fixed as soon as possible
instead of being allowed to persist.


This is not true from the user's perspective, though.


That depends on the user and the type of code involved. It's also
possible to install a default handler that attempts to sit in the middle
ground. Just for example, quite a few recent MS programs have a handler
that asks to email a bug report to them if an exception percolates out
far enough. Offhand, I don't know that they're using C++ exceptions for
this (it wouldn't surprise me if they use their own Structured Exception
Handling instead) but the idea is more or less independent of the
implementation.
Yes, I definitely agree that this is a problem. But one way to look at
it is that it's not really *your* problem. If you threw the exception,
you've done your part. If the caller ignores it, then they screwed up.
But this doesn't make any difference in many cases. If the program
breaks for the customer, you'll still be in trouble.
The point is that you really haven't "done your part." Exceptions are
intended to handle situations that are (or at least may be) recoverable.
Just for example, if a memory allocation fails, a program might attempt
to free up other memory that's caching various data, and then continue
with its job. Therefore, an exception works well.

Unless you have a system in which it at least MIGHT be possible to
recover from a contract violation, an exception is not an appropriate
way of dealing with the problem (an example would be if the program
responded to a contract violation by attempting to download an updated
version of the component that caused the problem).

[ ... ]
You could try to make an un-ignorable exception, maybe. Suppose your
exception object includes a 'handled' flag. On construction, this flag
is false. The catcher must set it to true. On destruction, if the flag
does not indicate that the exception was handled, the destructor aborts
the program. The problem is that it can still be ignored, it just takes
a little more effort (catch, set the flag, do nothing else).


I think we're mostly in agreement here -- ultimately, C++ doesn't
provide a single, obvious way that a contract violation _should_ always
be handled. I'm reasonably convinced that the usual exception mechanism
without any adornment (for lack of a better word) is rarely a good way
to handle it. The adornment you've given above might work nicely for
some situations, but I'm not at all sure that it's a universal solution
either.

That's not intended as a slam though: it may easily be that there simply
IS no universal solution to this problem, or at least that if there is
it's not a direct, simple and obvious one.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #25

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

Similar topics

235
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
7
by: Thomas | last post by:
I am compiling with g++ the fol. class: template<typename E> class C_vector_ : public std::vector<E> { private:
7
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
2
by: Steve555 | last post by:
Hi, In a function that erases part of a string, the compiler sometimes gives this error: terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::erase I...
5
by: aaragon | last post by:
Hi everyone, I wrote a very simple function to try to understand the casting of variables in C++. The function is function foo() { std::vector<inttest(100); randomize(test); unsigned long...
2
by: pssraju | last post by:
Hi, At present application was built on solaris 9 using sun studio 9 (Sun C++ 5.6) & rouguewave sorce pro 5. We are planning to port the same application onto SuSE Linux 9.5.0 using GCC 3.3.3 & RW...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.