Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2001-04-11 18:21:07


----- Original Message -----
From: <williamkempf_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Wednesday, April 11, 2001 6:43 PM
Subject: [boost] Re: any_function (callback) library

> --- In boost_at_y..., "David Abrahams" <abrahams_at_m...> wrote:
> >
> > ----- Original Message -----
> > From: <williamkempf_at_h...>
> >
> > > C++ platforms (MSVC) that throw exceptions when an assertion
> fires?
> > > Some alternative library assertion packages may do this, but none
> > > that come with MSVC throw exceptions. I was speaking of other
> > > languages here.
> >
> > What does this program do with msvc?
> >
> > include <cassert>
> > #include <iostream>
> >
> > int main() {
> > try {
> > assert(0);
> > }
> > catch(...) {
> > std::cout << "hi there" << std::endl;
> > }
> > return 0;
> > }
> >
> > Try compiling it as follows:
> > cl /MLd /GX /EHa foo.cpp
>
> It halts the program. Even if you Ignore the assertion the catch
> block is never entered. Like I said, none of the assertion libraries
> that come with MSVC throw exceptions.

That's incorrect. You obviously didn't try it. It throws a structured
exception which raises JIT debugging. First you get an abort/retry dialog.
Then you choose "Retry" to enter JIT debugging. Then it prints "hi there" to
standard out as the structured exception enters the catch block. Then the
JIT debugger fails to come up because the exception isn't re-thrown.

Now try the following 2 programs for comparison with the same command line
(cl /MLd /GX /EHa foo.cpp):

//------
include <cassert>
#include <iostream>

int main() {
    try {
        assert(0);
    }
    catch(...) {
        std::cout << "hi there" << std::endl;
        throw;
    }
    return 0;
}
//-------

include <cassert>
#include <iostream>

int main() {
    assert(0);
    std::cout << "hi there" << std::endl;
}

> Again, std::vector::at() exists for precisely this reason, and C++
> programmers use it in precisely the same manner as IndexError is
> used. I don't agree that we have a completely different culture in
> this case. There are many C++ programmers that want, even need, to
> insure their programs never crash... that they can catch errors,
> possibly handle them, and continue running.

...mmm, I don't think I agree. Would anybody really do this in C++?

for (std::size_t i = 0;; ++i)
{
    T* x;
    try {
        x = &v.at(i);
    }
    catch(std::out_of_range) {
        break;
    }
    f(*x);
}

This is the equivalent of what goes on in Python: the exception is used in
an ordinary way to detect the end of the loop. I think in C++ you would not
want to do it that way under any circumstances. For one thing, its
unweildly. Look how hard I had to work to isolate the exception and make
sure it really came from the indexing operation. IOW, I didn't write this:

for (std::size_t i = 0;; ++i)
{
    try {
        f(v.at(i));
    }
    catch(std::out_of_range) {
        break;
    }
}

I think vector::at() is used to deal with a certain kind of precondition
violation, and that's all.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk