Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   exception and streambuf (http://www.velocityreviews.com/forums/t744139-exception-and-streambuf.html)

mathieu 02-24-2011 05:48 PM

exception and streambuf
 
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::overflow (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::ostream wstr(&wbuf);
wstr << "Hello world!" << std::flush;
return 0;
}

James Kanze 02-25-2011 08:47 AM

Re: exception and streambuf
 
On Feb 24, 5:48 pm, mathieu <mathieu.malate...@gmail.com> 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::overflow (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::ostream 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

mathieu 02-25-2011 10:47 AM

Re: exception and streambuf
 
On Feb 25, 9:47*am, James Kanze <james.ka...@gmail.com> wrote:
> On Feb 24, 5:48 pm, mathieu <mathieu.malate...@gmail.com> 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::overflow (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::ostream 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

James Kanze 02-25-2011 05:40 PM

Re: exception and streambuf
 
On Feb 25, 10:47 am, mathieu <mathieu.malate...@gmail.com> wrote:
> On Feb 25, 9:47 am, James Kanze <james.ka...@gmail.com> 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

Paul 02-25-2011 06:36 PM

Re: exception and streambuf
 

"James Kanze" <james.kanze@gmail.com> wrote in message
news:65798818-10bc-4482-8b72-6a1e947b3342@d12g2000vbz.googlegroups.com...
> On Feb 25, 10:47 am, mathieu <mathieu.malate...@gmail.com> wrote:
>> On Feb 25, 9:47 am, James Kanze <james.ka...@gmail.com> 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
>




All times are GMT. The time now is 06:06 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.