Throwing exception from derived streambuf::underflow ...

Throwing exception from derived streambuf::underflow ...

Post by Jasp » Wed, 20 Nov 2002 01:17:07



Hi,

I have a class that is derived from std::streambuf. In my derived
underflow function I want to throw an exception if an error occured
reading from the data source (in this case a tcp socket). However the
exception I throw is being caught somewhere in the std:: library
itself. This means my program continues running as if nothing has
happened :/ Is there any way to avoid the std library from catching my
user-thrown exception ?

Thx, Jasper

 
 
 

Throwing exception from derived streambuf::underflow ...

Post by Ron Natali » Wed, 20 Nov 2002 01:32:35



> Hi,

> I have a class that is derived from std::streambuf. In my derived
> underflow function I want to throw an exception if an error occured
> reading from the data source (in this case a tcp socket). However the
> exception I throw is being caught somewhere in the std:: library
> itself. This means my program continues running as if nothing has
> happened :/ Is there any way to avoid the std library from catching my
> user-thrown exception ?

Don't use the Dinkumware libraries.

 
 
 

Throwing exception from derived streambuf::underflow ...

Post by John Harriso » Wed, 20 Nov 2002 05:02:00



Quote:> Hi,

> I have a class that is derived from std::streambuf. In my derived
> underflow function I want to throw an exception if an error occured
> reading from the data source (in this case a tcp socket). However the
> exception I throw is being caught somewhere in the std:: library
> itself. This means my program continues running as if nothing has
> happened :/ Is there any way to avoid the std library from catching my
> user-thrown exception ?

> Thx, Jasper

A user thrown exception will be caught and will set the badbit of the
stream. If you want that exception to be rethrown call

stream.exceptions(std::ios_base::badbit);

before you start doing I/O. As Ron suggests this advice might be specific to
Dinkumware (I wouldn't know).

john

 
 
 

Throwing exception from derived streambuf::underflow ...

Post by P.J. Plauge » Wed, 20 Nov 2002 09:06:36




Quote:> > Hi,

> > I have a class that is derived from std::streambuf. In my derived
> > underflow function I want to throw an exception if an error occured
> > reading from the data source (in this case a tcp socket). However the
> > exception I throw is being caught somewhere in the std:: library
> > itself. This means my program continues running as if nothing has
> > happened :/ Is there any way to avoid the std library from catching my
> > user-thrown exception ?

> Don't use the Dinkumware libraries.

Well, using a library that doesn't conform to the C++ Standard is one
way to avoid standard conforming behavior that you don't want. Another
is to learn how to write conforming code that does what you want.
See ios_base<Char>::exceptions as a way to ensure that exceptions
propagate past I/O calls.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

 
 
 

Throwing exception from derived streambuf::underflow ...

Post by P.J. Plauge » Wed, 20 Nov 2002 09:07:18





> > Hi,

> > I have a class that is derived from std::streambuf. In my derived
> > underflow function I want to throw an exception if an error occured
> > reading from the data source (in this case a tcp socket). However the
> > exception I throw is being caught somewhere in the std:: library
> > itself. This means my program continues running as if nothing has
> > happened :/ Is there any way to avoid the std library from catching my
> > user-thrown exception ?

> > Thx, Jasper

> A user thrown exception will be caught and will set the badbit of the
> stream. If you want that exception to be rethrown call

> stream.exceptions(std::ios_base::badbit);

> before you start doing I/O. As Ron suggests this advice might be specific to
> Dinkumware (I wouldn't know).

It's specific to standard-conforming C++ libraries.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

 
 
 

Throwing exception from derived streambuf::underflow ...

Post by tom_usen » Wed, 20 Nov 2002 19:07:22



Quote:>Hi,

>I have a class that is derived from std::streambuf. In my derived
>underflow function I want to throw an exception if an error occured
>reading from the data source (in this case a tcp socket). However the
>exception I throw is being caught somewhere in the std:: library
>itself. This means my program continues running as if nothing has
>happened :/ Is there any way to avoid the std library from catching my
>user-thrown exception ?

Yes.

mystream.exceptions(std::ios_base::badbit);

Now your exception should propogate correctly. This is as required by
the standard, as far as I can tell, and seems to work with the
Dinkumware and libcomo.

Some other libraries (GCC 3.2 for one) seem non-conforming in this
respect, propogating the exception regardless of the exception mask.

Tom

 
 
 

Throwing exception from derived streambuf::underflow ...

Post by tom_usen » Wed, 20 Nov 2002 19:09:19





>> Hi,

>> I have a class that is derived from std::streambuf. In my derived
>> underflow function I want to throw an exception if an error occured
>> reading from the data source (in this case a tcp socket). However the
>> exception I throw is being caught somewhere in the std:: library
>> itself. This means my program continues running as if nothing has
>> happened :/ Is there any way to avoid the std library from catching my
>> user-thrown exception ?

>Don't use the Dinkumware libraries.

Or any other standard conforming ones.

But it might be better to take a look at 27.6.1.1 /4 instead.

Tom

 
 
 

1. streambuf::underflow dislikes exceptions

Hi,

I'm writing a streambuf, but I'm having some problems with handling
errors. My streambuf may fail in underflow, and according to the C++
standard draft (jan 96), that can be handled by throwing an exception:

   12) undeflow or uflow might fail by throwing an exception
prematurely.
   The  intention  is  not  only that the calls will not return eof()
but
   that they will return ``immediately.''

The problem is that my program dumps core if I throw an exception in
underflow().

Here's the stack trace:

#0  0x400a6d41 in __kill () from /lib/libc.so.6
#1  0x400a69b6 in raise (sig=6) at ../sysdeps/posix/raise.c:27
#2  0x400a80d8 in abort () at ../sysdeps/generic/abort.c:88
#3  0x40057e55 in __default_terminate () from
/usr/lib/libstdc++-libc6.1-1.so.2
#4  0x40057e72 in __terminate () from /usr/lib/libstdc++-libc6.1-1.so.2
#5  0x4005875b in __throw () from /usr/lib/libstdc++-libc6.1-1.so.2
#6  0x804a8fd in fcol::mystreambuf::underflow () at mystreambuf.cpp:63
#7  0x400debcc in __underflow (fp=0x8055568) at genops.c:303
#8  0x400dee83 in _IO_default_xsgetn (fp=0x8055568, data=0xbffff89a,
n=1) at genops.c:472
#9  0x40052242 in streambuf::xsgetn (this=0x8055568, s=0xbffff89a

#10 0x400df1f6 in _IO_sgetn (fp=0x8055568, data=0xbffff89a, n=1) at
genops.c:431
#11 0x4004d641 in istream::read (this=0xbffff8a4, s=0xbffff89a

#12 0x804a23f in main () at test_imystream.cpp:25

It seems that #10, #8 and #7 are functions written in C. Is it ok for a
C++ (member) function to throw an exception, if it is called by a C
function, that is called by a C++ (member) function, that catches the
exception? Any other ideas as to why this may be happening? I'll e glad
to provide more detailed information upon request.

I'm using gcc (egcs-2.91.66).

best regards,

Thomas

2. Palm III on Ebay, like new!

3. Throwing exceptions in dtors called by thrown exceptions

4. Switch from Schedule Plus to Outlook 98?

5. Derived Class function throwing exceptions

6. need info about Synopsys chip synthesis workshops

7. Streambuf uflow() vs underflow()

8. Official IBM notice re FixPak 16

9. C++ Exceptions: throw vs. throw new

10. Exceptions are good...but to throw or not to throw is the problem

11. Exception Objects Throwing Exceptions.

12. Exception while throwing an exception?

13. Overflow, underflow, and exceptions