exception and streambuf

Discussion in 'C++' started by mathieu, Feb 24, 2011.

  1. mathieu

    mathieu Guest

    Dear c++ gurus,

    I am trying to understand why the following piece of code is not
    throwing any exception on my system. This is a simplified version just
    for the demo (*). My implementation of sync always throw just for this
    example. I am using Linux/g++ 4.4.5/debian.

    Thanks for any clue,

    (*)
    #include <iostream>
    #include <string>

    int write_to_stdout(const char* text, size_t length)
    {
    std::string s ( text, text + length);
    std::cout << s << std::endl;
    return length;
    }

    class windowbuf : public std::streambuf {
    public:
    int sync ();
    int overflow (int ch);
    };

    int windowbuf::sync ()
    {
    throw "Killing Program"; // where did it go ?
    return windowbuf::traits_type::eof();
    }

    int windowbuf::eek:verflow (int ch)
    {
    std::streamsize n = pptr () - pbase ();
    char buffer;
    buffer = ch;
    write_to_stdout( &buffer, 1 );
    pbump (-n); // Reset pptr().
    return 0;
    }

    int
    main (int argc, char*argv[])
    {
    windowbuf wbuf;
    std::eek:stream wstr(&wbuf);
    wstr << "Hello world!" << std::flush;
    return 0;
    }
    mathieu, Feb 24, 2011
    #1
    1. Advertising

  2. mathieu

    James Kanze Guest

    On Feb 24, 5:48 pm, mathieu <> wrote:
    > Dear c++ gurus,
    >
    > I am trying to understand why the following piece of code is not
    > throwing any exception on my system. This is a simplified version just
    > for the demo (*). My implementation of sync always throw just for this
    > example. I am using Linux/g++ 4.4.5/debian.


    > Thanks for any clue,


    > (*)
    > #include <iostream>
    > #include <string>


    > int write_to_stdout(const char* text, size_t length)
    > {
    > std::string s ( text, text + length);
    > std::cout << s << std::endl;
    > return length;
    > }


    > class windowbuf : public std::streambuf {
    > public:
    > int sync ();
    > int overflow (int ch);
    > };


    > int windowbuf::sync ()
    > {
    > throw "Killing Program"; // where did it go ?
    > return windowbuf::traits_type::eof();
    > }


    > int windowbuf::eek:verflow (int ch)
    > {
    > std::streamsize n = pptr () - pbase ();
    > char buffer;
    > buffer = ch;
    > write_to_stdout( &buffer, 1 );
    > pbump (-n); // Reset pptr().
    > return 0;
    > }


    > int
    > main (int argc, char*argv[])
    > {
    > windowbuf wbuf;
    > std::eek:stream wstr(&wbuf);
    > wstr << "Hello world!" << std::flush;
    > return 0;
    > }


    What makes you sure that it's not throwing an exception? The
    exception should be caught in ostream, and converted into
    setting badbit, unless the implementation is defective. You
    don't test badbit, so you cannot see if it was thrown or not;
    I think you'll find that your output failed. Similarly, if you
    set exceptions(std::badbit) on the ostream, you'll get an
    exception (but not the one you threw).

    This means that there is no way of propagating specific error
    information up by means of an exception; if you need specific
    information concerning the error at a higher level, the only
    solution is to store it in the streambuf, and read it from there
    when the higher level detects the error.

    --
    James Kanze
    James Kanze, Feb 25, 2011
    #2
    1. Advertising

  3. mathieu

    mathieu Guest

    On Feb 25, 9:47 am, James Kanze <> wrote:
    > On Feb 24, 5:48 pm, mathieu <> wrote:
    >
    >
    >
    >
    >
    > > Dear c++ gurus,

    >
    > >   I am trying to understand why the following piece of code is not
    > > throwing any exception on my system. This is a simplified version just
    > > for the demo (*). My implementation of sync always throw just for this
    > > example. I am using Linux/g++ 4.4.5/debian.
    > > Thanks for any clue,
    > > (*)
    > > #include <iostream>
    > > #include <string>
    > > int write_to_stdout(const char* text, size_t length)
    > > {
    > >   std::string s ( text, text + length);
    > >   std::cout << s << std::endl;
    > >   return length;
    > > }
    > > class windowbuf : public std::streambuf {
    > >   public:
    > >     int sync ();
    > >     int overflow (int ch);
    > > };
    > > int windowbuf::sync ()
    > > {
    > >   throw "Killing Program"; // where did it go ?
    > >   return windowbuf::traits_type::eof();
    > > }
    > > int windowbuf::eek:verflow (int ch)
    > > {
    > >   std::streamsize n = pptr () - pbase ();
    > >   char buffer;
    > >   buffer = ch;
    > >   write_to_stdout( &buffer, 1 );
    > >   pbump (-n);  // Reset pptr().
    > >   return 0;
    > > }
    > > int
    > > main (int argc, char*argv[])
    > > {
    > >   windowbuf wbuf;
    > >   std::eek:stream wstr(&wbuf);
    > >   wstr << "Hello world!" << std::flush;
    > >   return 0;
    > > }

    >
    > What makes you sure that it's not throwing an exception?  The
    > exception should be caught in ostream, and converted into
    > setting badbit, unless the implementation is defective.  You
    > don't test badbit, so you cannot see if it was thrown or not;
    > I think you'll find that your output failed.  Similarly, if you
    > set exceptions(std::badbit) on the ostream, you'll get an
    > exception (but not the one you threw).
    >
    > This means that there is no way of propagating specific error
    > information up by means of an exception; if you need specific
    > information concerning the error at a higher level, the only
    > solution is to store it in the streambuf, and read it from there
    > when the higher level detects the error.


    Once again you are right. I did not check if my ostream was still
    valid. Any later operations seems to be properly discarded.
    Thanks James.

    BTW, would it be possible to resurrect your pages:
    http://kanze.james.neuf.fr/articles-en.html
    I kept finding those reference while googling for help on 'streambuf
    subclass'.

    Thanks again
    mathieu, Feb 25, 2011
    #3
  4. mathieu

    James Kanze Guest

    On Feb 25, 10:47 am, mathieu <> wrote:
    > On Feb 25, 9:47 am, James Kanze <> wrote:


    [...]
    > BTW, would it be possible to resurrect your
    > pages:http://kanze.james.neuf.fr/articles-en.html I kept
    > finding those reference while googling for help on 'streambuf
    > subclass'.


    I'm in the process of reorganizing my life a bit; I don't know
    where I'll finally have a home (page). Once I do, one of the
    first things I'll do is resurrect my old site (with an updated
    version of the library, provided my employers don't object).

    --
    James Kanze
    James Kanze, Feb 25, 2011
    #4
  5. mathieu

    Paul Guest

    "James Kanze" <> wrote in message
    news:...
    > On Feb 25, 10:47 am, mathieu <> wrote:
    >> On Feb 25, 9:47 am, James Kanze <> wrote:

    >
    > [...]
    >> BTW, would it be possible to resurrect your
    >> pages:http://kanze.james.neuf.fr/articles-en.html I kept
    >> finding those reference while googling for help on 'streambuf
    >> subclass'.

    >
    > I'm in the process of reorganizing my life a bit;

    More details please, this could be very good material for my TV show.

    > I don't know
    > where I'll finally have a home (page). Once I do, one of the
    > first things I'll do is resurrect my old site (with an updated
    > version of the library, provided my employers don't object).
    >
    > --
    > James Kanze
    >
    Paul, Feb 25, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Viktor_Lundstr=F6m?=

    Exception handling vs. streambuf

    =?ISO-8859-1?Q?Viktor_Lundstr=F6m?=, Sep 29, 2003, in forum: C++
    Replies:
    0
    Views:
    613
    =?ISO-8859-1?Q?Viktor_Lundstr=F6m?=
    Sep 29, 2003
  2. Henry

    Streambuf and streams

    Henry, Aug 31, 2004, in forum: C++
    Replies:
    1
    Views:
    403
    tom_usenet
    Aug 31, 2004
  3. Peter Jansson
    Replies:
    1
    Views:
    511
    David Rubin
    Nov 8, 2004
  4. Johannes Barop
    Replies:
    1
    Views:
    433
    JH Trauntvein
    Dec 29, 2005
  5. Christopher Pisz
    Replies:
    2
    Views:
    598
    James Kanze
    Dec 12, 2007
Loading...

Share This Page