• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<HTML>
3<HEAD>
4    <TITLE>Exceptions</TITLE>
5    <LINK REL="stylesheet" HREF="../../../../boost.css">
6    <LINK REL="stylesheet" HREF="../theme/iostreams.css">
7    <STYLE>
8        OL LI { list-style-image:none; list-style-type: decimal }
9        OL LI LI { list-style-image:none; list-style-type: lower-alpha }
10     </STYLE>
11</HEAD>
12<BODY>
13
14<!-- Begin Banner -->
15
16    <H1 CLASS="title">User's Guide</H1>
17    <HR CLASS="banner">
18
19<!-- End Banner -->
20
21<!-- Begin Nav -->
22
23<DIV CLASS='nav'>
24     <A HREF='views.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/prev.png'></A>
25    <A HREF='guide.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/up.png'></A>
26    <A HREF='buffering.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/next.png'></A>
27</DIV>
28
29<!-- End Nav -->
30
31<A NAME="overview"></A>
32<H2>3.10 Exceptions</H2>
33
34<DL class="page-index">
35  <DT><A href="#standard_iostreams">The Standard Iostreams library</A></DT>
36  <DT><A href="#boost_iostreams">The Boost Iostreams library</A></DT>
37  <DT><A href="#safety">Exception Safety</A></DT>
38  <DT><A href="#acknowledgments">Acknowledgments</A></DT>
39</DL>
40
41<HR STYLE="margin-top:1em">
42
43<A NAME="standard_iostreams"></A>
44<H2>The Standard Iostreams library</H2>
45
46<P>
47    The <CODE>public</CODE> member functions of <CODE>std::basic_streambuf</CODE> which perform i/o &#8212; <CODE>sgetn()</CODE>, <CODE>sputn()</CODE>, <CODE>pubsync(), <I>etc.</I> </CODE> &#8212; are implemented using the <CODE>protected virtual</CODE> interface of <CODE>std::basic_streambuf</CODE> &#8212; <CODE>underflow()</CODE>, <CODE>overflow()</CODE>, <CODE>sync()</CODE>, <I>etc</I>. Most of these <CODE>protected virtual</CODE> functions are allowed to signal failure either by throwing an exception or by returning an error indicator (often <CODE>traits_type::eof()</CODE>). All exceptions thrown are propogated by the <CODE>public</CODE> member functions, so users of raw stream buffers must in principle be prepared to handle exceptions. However, the stream buffers which ship with most standard library implementations do not throw exceptions.
48</P>
49
50<P>
51    Standard streams, by contrast, throw no exceptions by default: users must set the <I>exception mask</I> to request that exceptional conditions be reported using exceptions. If <CODE>ios_base::badbit</CODE> is set in a stream's exception mask, all exceptions thrown by the underlying stream buffer will be caught and rethrown.
52</P>
53
54<P>
55    With streams, therefore, the user has a choice whether to enable exceptions; with stream buffers, the implementor has a choice, but the user has none.
56</P>
57
58<A NAME="boost_iostreams"></A>
59<H2>The Boost Iostreams library</H2>
60
61<H5>Policy</H5>
62
63<P>
64    The fundamental stream buffer template in the Boost Iostreams library, <A HREF="generic_streams.html#stream_buffer"><CODE>stream_buffer</CODE></A>, implements the <CODE>protected virtual</CODE> memeber functions of <CODE>std::basic_streambuf</CODE> by delegating to the i/o functions <A HREF="../functions/read.html"><CODE>read</CODE></A>, <A HREF="../functions/write.html"><CODE>write</CODE></A>, <A HREF="../functions/put.html"><CODE>put</CODE></A>, <A HREF="../functions/get.html"><CODE>get</CODE></A>, <I>etc.</I>, which in turn delegate to member functions of Filters and Devices. <I>These member functions throw exceptions to report errors.</I> The exceptions are propagated by the <CODE>protected virtual</CODE> members of <CODE>stream_buffer</CODE> and by the public members of <CODE>std::basic_streambuf</CODE>.
65</P>
66
67<P>
68    It is recommended that all exceptions thrown by user-defined Filters and Devices
69    derive from <CODE>std::ios_base::failure</CODE>.<SUP><A CLASS="footnote_ref" NAME="note_1_ref" HREF="#note_1">[1]</A></SUP>
70</P>
71
72<H5>Rationale</H5>
73
74<P>
75    An alternative formulation would have been to require Filter and Device member functions with return type <CODE>std::streamsize</CODE> to return <CODE>-1</CODE> to indicate errors, and to modify the specification of functions returning <CODE>void</CODE> so that they return a <CODE>bool</CODE> indicating success. The decision to rely on exceptions to report errors was made to allow implementors a means to convey aditional information about the cause of errors, and to free them from having to remember which return values are designated as error indicators.
76</P>
77
78<P>
79    A third possibility would have been to follow the example of <CODE>std::basic_streambuf</CODE> and allow member functions of Filters and Devices to signal errors either by throwing exceptions or by returning designated error indicators. This was rejected because it would have complicated the specifications of the various <A HREF="concepts.html#filter_concepts">Filter</A> and <A HREF="concepts.html#device_concepts">Device</A> concepts and made the internals of <CODE>stream_buffer</CODE> more difficult to understand and maintain.<SUP><A CLASS="footnote_ref" NAME="note_2_ref" HREF="#note_2">[2]</A></SUP>
80</P>
81
82<A NAME="safety"></A>
83<H2>Exception Safety</H2>
84
85The Iostreams library aims to provide to following guarantees if an exception is thrown during an i/o operation:
86
87<OL>
88    <LI>Resources are freed by calling destructors or <A HREF="../functions/close.html"><CODE>close</CODE></A>, if appropriate.
89    <LI>
90        The invariants specified by the C++ standard library for streams and stream buffers are maintained. Attempting to perform additional i/o is allowed but may cause further exceptions.
91    </LI>
92    <LI>The invariants specified by the Boost Iostreams library for streams and stream buffers are maintained. Specifically,
93        <OL>
94            <LI>
95                A <A HREF="generic_streams.html#stream_buffer"><CODE>stream_buffer</CODE></A> or <A HREF="generic_streams.html#stream"><CODE>stream</CODE></A> can be closed and reopened to perform additional i/o with a new instance of the underlying <A HREF="concepts.html#filter_concepts">Filter</A> or <A HREF="concepts.html#device_concepts">Device</A> type.
96            </LI>
97            <LI>
98                A <A HREF="../classes/filtering_streambuf.html"><CODE>filtering_streambuf</CODE></A>'s or <A HREF="../classes/filtering_stream.html"><CODE>filtering_stream</CODE></A>'s chain of Filters and Devices is in a consistent state. If the current Device is removed from the chain and a new one added the user may perform i/o as if the the stream or stream buffer were newly constructed.
99            </LI>
100        </OL>
101    </LI>
102</OL>
103
104<P>
105    Conditions 2. and 3. rely on the specification of the <A HREF="concepts.html#filter_concepts">Filter</A> and <A HREF="concepts.html#device_concepts">Device</A> concepts. Note that there is <I>no</I> guarantee that a stream's or stream buffer's character sequences are in a consistent state, <I>i.e.</I>, that data has not been corrupted, and no way in general to determine the state of these sequences after an exception.
106</P>
107
108<P>
109    These conditions amount to what is known as the <I>basic guarantee</I> of exception safety (<I>see</I> <A CLASS="bib_ref" HREF="../bibliography.html#abrahams1">[Abrahams1]</A>.)
110</P>
111
112<A NAME="acknowledgments"></A>
113<H2>Acknowledgments</H2>
114
115<P>Thanks to Angelika Langer and John Torjo for discussion of exceptions.</P>
116
117<!-- Begin Footnotes -->
118
119<HR>
120
121<P>
122    <A CLASS="footnote_ref" NAME="note_1" HREF="#note_1_ref"><SUP>[1]</SUP></A>The C++ standard describes <CODE>std::ios_base::failure</CODE> as "the base class for the types of all objects thrown as exceptions, by functions in the Iostreams library, to report errors detected during stream buffer operations." (<A CLASS="bib_ref" HREF="../bibliography.html#iso">[ISO]</A>, section 27.4.2.1.1.) There is a difference of opinion among commentators whether this means that <I>all</I> exceptions thrown by stream buffer member functions must be of type <CODE>failure</CODE>.
123</P>
124
125<P>
126    A leading text on C++ iostreams interprets the language in the standard to mean that exceptions "may be of any type" but that "if the exception is rasied due to an error situation <I>discovered by any of the IOStreams operations</I>, it is of the type <CODE>failure</CODE>" (<A CLASS="bib_ref" HREF="../bibliography.html#langer">[Langer]</A>, p. 36, emphasis added). As an example, the authors state that a <CODE>std::bad_alloc</CODE> generated during buffer allocation need not be caught and rethrown as a <CODE>failure</CODE>.
127</P>
128
129<P>
130    Another leading text, however, takes the view that <I>all</I> exceptions must derive
131    from <CODE>failure</CODE>. (<A CLASS="bib_ref" HREF="../bibliography.html#josuttis1">[Josuttis1]</A>, p. 602.)
132</P>
133<P>
134    The Iostreams library takes a neutral position on this matter: all exceptions thrown
135    internally by the library are of type <CODE>failure</CODE> or a derived class, but exceptions
136    thrown by user-defined types are propogated <I>as-is</I>.
137</P>
138
139<P>
140    <A CLASS="footnote_ref" NAME="note_2" HREF="#note_2_ref"><SUP>[2]</SUP></A>As Herb Sutter writes, "it is always poor design to allow an operation to report the same error in two different ways....It complicates the interface and the semantics. And it makes the caller's life harder because the caller must be able to handle both flavors of error reporting." (<A CLASS="bib_ref" HREF="../bibliography.html#sutter">[Sutter]</A>, p. 130.)
141</P>
142
143<!-- End Footnotes -->
144
145<!-- Begin Footer -->
146
147<HR>
148
149<P CLASS="copyright">&copy; Copyright 2008 <a href="http://www.coderage.com/" target="_top">CodeRage, LLC</a><br/>&copy; Copyright 2004-2007 <a href="https://www.boost.org/users/people/jonathan_turkanis.html" target="_top">Jonathan Turkanis</a></P>
150<P CLASS="copyright">
151    Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <A HREF="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>)
152</P>
153
154<!-- End Footer -->
155
156</BODY>