473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Exception handling vs. streambuf

Hi!
I recently decided to write a streambuf which handles streamed
IO to a device (ie. a socket).
Now, in an effort to move away from C-style IO error handling
(ie. if(read(..) == -1) ...), I decided to make my streambuf raise
exceptions upon device error states.
The code is supposed to run on several platforms, but there seems
to be some inconsistency between the different iostream
implementations. This led me to believe that I'm probably doing
this all wrong.
On win32, if an exception is thrown in underflow/overflow the
exception is encased in a try-catch(...) statement in iostream
and 'eaten', unless the iostream.exceptions(badbit) is set,
in which case my exception is rethrown in its entirety (which
is what I want).
The same code, on Solaris gcc 3.01, results in an exception
always being thrown regardless of iostream.exceptions(badbit).
On RedHat 7.2 (don't know which gcc version) I get a SIGABRT,
regardless of badbit flag.

Should I not be throwing exceptions in the streambuffer?
If so, how do I raise an exception upon error?
On overflow, returning EOF with iostream.exceptions(eofbit) set
does not result in exception on RedHat.

See example code below:

#include <iostream>

using namespace std;

class mybuf: public streambuf {
char oBuf[1024];

public:
static int lastExceptionPtr;

mybuf() {
setp(oBuf, oBuf+1);
}

~mybuf() {
sync();
}

int sync() {
if (pptr() > pbase()) {
// physical write
setp(oBuf, oBuf+1);
}
return 0;
}

int overflow(int c) {
sync();
string *str = new string("exception in overflow");
lastExceptionPtr = (int)str;
throw str;

if (c != EOF) {
*pptr() = static_cast<char>(c);
pbump(1);
}
return c;
}
};

class myio : public ostream {
mybuf buf;

public:
myio() : ostream(&buf) {
}
};

min.cpp:
#include "min.h"

int mybuf::lastExceptionPtr = -1;

int main(int argc, char *argv[]) {
myio stream;
//stream.exceptions(iostream::goodbit); // throw no
exceptions, ever
cout << "Testing throw of exception inside overflow, should
not throw exceptions..." << endl;
try {
stream << "0";
stream << "1";
stream << "2";
stream.flush();
} catch(string *ptr) {
cout << "Stream threw exception, even though it
should not!" << endl;
cout << "Was the exception equal to the one thrown? "
<< ((int)ptr == mybuf::lastExceptionPtr) << endl;
} catch(...) {
cout << "Stream threw undefined exception, even
though it should not! " << endl;
}
cout << "Test done." << endl;
mybuf::lastExceptionPtr = -1;

cout << "Testing throw of exception inside overflow, should
throw exceptions..." << endl;

myio stream2;
stream2.exceptions(iostream::badbit);
try {
stream2 << "0";
stream2 << "1";
stream2 << "2";
stream2.flush();
} catch(string *ptr) {
cout << "Stream threw exception when it should!" <<
endl;
cout << "Was the exception equal to the one thrown? "
<< ((int)ptr == mybuf::lastExceptionPtr) << endl;
}

cout << "Test done." << endl;
return 0;
}
Viktor Lundström

Jul 19 '05 #1
0 1909

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Viktor Lundström | last post by:
Hi! I was planning to wrap a socket inside an iostream, to achieve something like this: TCPSocket s(..); s << "Hello!" << endl; Information on the web seems to be a bit scarce on how to do...
9
by: Fred Ma | last post by:
Hello, I posted previously under the thread: How to break this up into streambuf/ostream I've asked our library to get "C++ IOStreams and Locales..." by A. Langer et al. Meantime, I've...
9
by: Marcin Kalicinski | last post by:
Hi, I have a set of C-like functions for file access (like fopen, fwrite, fread, fseek etc.). But I want to access the files using C++ stream, not these functions. What I probably need to do is...
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
2
by: Raf256 | last post by:
Hello, my custom streambuf works fine with output via << and with input via .get() but fails to input via >> or getline... any idea why? -------------------- A custom stream buffer (for...
7
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap...
0
by: tryptik | last post by:
All- I have been looking at all the info I can find on deriving streambuf objects, but I need some advice. I wish to derive a custom streambuf that filters out c++ style comments. I have...
4
by: rakesh.usenet | last post by:
For a particular application of mine - I need a simulation of byte array output stream. * write data onto a stream * getback the contiguous content as an array later for network transport. ...
3
by: Raymond Martineau | last post by:
I have the following code segment for a class intended to split output between cout and a file: class SplitStream : public std::streambuf { std::streambuf *x; public: SplitStream() {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.