File: iostreams.yo

package info (click to toggle)
c%2B%2B-annotations 12.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,044 kB
  • sloc: cpp: 24,337; makefile: 1,517; ansic: 165; sh: 121; perl: 90
file content (32 lines) | stat: -rw-r--r-- 1,813 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
The bf(C++) I/O library was used well before exceptions were available in
bf(C++). Hence, normally the classes of the iostream library do not throw
exceptions. However, it is possible to modify that behavior using the
 ti(ios::exceptions) member function. This function has two overloaded
versions:
    itemization(
    ithtq(exceptions (function))(ios::iostate exceptions())
        (hi(iostate)this member returns the i(state flags) for which the
         stream will throw exceptions;)
    itt(void exceptions(ios::iostate state))
        quote(hi(iostate)this member causes the stream to throw an exception
            when state tt(state) is observed.)
    )
    In the I/O library, exceptions are objects of the class
 hi(failure class) tt(ios::failure), derived from hi(exception class)
tt(ios::exception). A tt(std::string const &message) may be specified when
defining a tt(failure) object. Its message may then be  retrieved using its
 hi(what) tt(virtual char const *what() const) member.

    Exceptions should be used in exceptional circumstances. Therefore, we
think it is questionable to have stream objects throw exceptions for fairly
normal situations like endOfFile(). Using exceptions to handle input errors
might be defensible (e.g., in situations where input errors should not occur
and imply a corrupted file) but often aborting the program with an appropriate
error message would probably be the more appropriate action. As an example
consider the following interactive program using exceptions to catch incorrect
input:
        verbinclude(-a examples/ioexceptions.cc)

By default, exceptions raised from within tt(ostream) objects are caught by
these objects, which set their tt(ios::badbit) as a result. See also the
link(paragraph on this issue)(STRBUFLABEL) in section ref(STREAMBUF).