exception and streambuf

M

mathieu

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;
}
 
J

James Kanze

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.
 
M

mathieu

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
 
J

James Kanze

[...]
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top