Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > exception and streambuf

Reply
Thread Tools

exception and streambuf

 
 
mathieu
Guest
Posts: n/a
 
      02-24-2011
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: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:stream wstr(&wbuf);
wstr << "Hello world!" << std::flush;
return 0;
}
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      02-25-2011
On Feb 24, 5:48 pm, mathieu <(E-Mail Removed)> 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: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: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
 
Reply With Quote
 
 
 
 
mathieu
Guest
Posts: n/a
 
      02-25-2011
On Feb 25, 9:47*am, James Kanze <(E-Mail Removed)> wrote:
> On Feb 24, 5:48 pm, mathieu <(E-Mail Removed)> 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: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: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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-25-2011
On Feb 25, 10:47 am, mathieu <(E-Mail Removed)> wrote:
> On Feb 25, 9:47 am, James Kanze <(E-Mail Removed)> 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
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-25-2011

"James Kanze" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 25, 10:47 am, mathieu <(E-Mail Removed)> wrote:
>> On Feb 25, 9:47 am, James Kanze <(E-Mail Removed)> 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
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::streambuf::setp and std::streambuf::epptr() Christopher Pisz C++ 2 12-12-2007 12:15 PM
Problem with std::ostream, std::streambuf and "virtual xsputn" Johannes Barop C++ 1 12-29-2005 11:09 PM
std::streambuf::setg & std::streambuf::setp success or not? Peter Jansson C++ 1 11-08-2004 01:55 AM
Streambuf and streams Henry C++ 1 08-31-2004 04:04 PM
Exception handling vs. streambuf =?ISO-8859-1?Q?Viktor_Lundstr=F6m?= C++ 0 09-29-2003 05:45 PM



Advertisments