61. Ambiguity in iostreams exception policy

Section: 31.7.5.4 [istream.unformatted] Status: TC1 Submitter: Matt Austern Opened: 1998-08-06 Last modified: 2016-01-28

Priority: Not Prioritized

View all other issues in [istream.unformatted].

View all issues with TC1 status.

Discussion:

The introduction to the section on unformatted input (27.6.1.3) says that every unformatted input function catches all exceptions that were thrown during input, sets badbit, and then conditionally rethrows the exception. That seems clear enough. Several of the specific functions, however, such as get() and read(), are documented in some circumstances as setting eofbit and/or failbit. (The standard notes, correctly, that setting eofbit or failbit can sometimes result in an exception being thrown.) The question: if one of these functions throws an exception triggered by setting failbit, is this an exception "thrown during input" and hence covered by 27.6.1.3, or does 27.6.1.3 only refer to a limited class of exceptions? Just to make this concrete, suppose you have the following snippet.

  
  char buffer[N];
  istream is;
  ...
  is.exceptions(istream::failbit); // Throw on failbit but not on badbit.
  is.read(buffer, N);

Now suppose we reach EOF before we've read N characters. What iostate bits can we expect to be set, and what exception (if any) will be thrown?

Proposed resolution:

In 27.6.1.3, paragraph 1, after the sentence that begins "If an exception is thrown...", add the following parenthetical comment: "(Exceptions thrown from basic_ios<>::clear() are not caught or rethrown.)"

Rationale:

The LWG looked to two alternative wordings, and choose the proposed resolution as better standardese.