throwing errors in derived streambufs

K

Kevin Saff

I would like to be able to create a std::exception from a user-defined
stream, like:

error << "Error: " << filename_ << " not found!" << std::endl;

I figured I would just create a simple streambuf that would store characters
in a string until it received '\n' and then throw a std::exception with the
contents of the string. However, this exception is caught in the stream
implementation and translated into a badbit.

Any suggestions on how to do this? Do I just need to avoid deriving from
ostream and streambuf, and create a class with its own op <<?
 
K

Kevin Saff

Kevin Saff said:
I figured I would just create a simple streambuf that would store characters
in a string until it received '\n' and then throw a std::exception with the
contents of the string. However, this exception is caught in the stream
implementation and translated into a badbit.

OK, just after posting this I realized all that was necessary was to call
exceptions (std::ios::badbit) on the stream object. Sorry for the waste.
 

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

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,373
Latest member
Desiree036

Latest Threads

Top