<html>

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Error Handling Design Guidelines</title>
<link rel="stylesheet" type="text/css" href="../../../doc/html/minimal.css">
</head>

<body>

<p>Document number: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; N2809=08-0319<br>
Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%Y-%m-%d" startspan -->2008-12-05<!--webbot bot="Timestamp" endspan i-checksum="12188" --><br>
Project:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Programming Language C++, Library Working Group<br>
Reply-to:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Beman Dawes &lt;bdawes at acm.org&gt;<br>
&nbsp;</p>

<h1>Library Support for hybrid error handling</h1>
<h2>Introduction</h2>
<p>This proposal provides standard library clause 17 support for functions that allow 
users to choose whether errors should be reported via exception or via <code>
std::error_code</code> object.</p>
<p>The impact is to ensure that a function specified like this:</p>
<blockquote>
  <pre>void f(error_code&amp; ec=<i>unspecified-name</i>);</pre>
</blockquote>
<p>acts as if it had been specified like this:</p>
<blockquote>
  <pre>void f();                // throws on error
void f(error_code&amp; ec);  // never throws</pre>
</blockquote>
<p>The benefit of a single signature providing both throwing and error reporting 
semantics is that it eliminates a doubling of the number of signatures. This is 
particularly beneficial where a function already has an uncomfortably large 
number of overloads. The intent is that this defaulted argument approach be applied uniformly 
throughout the standard library because it is less confusing if all hybrid error handling 
interfaces are consistent.</p>
<p>The proposal makes no change to any current standard library components. It 
just adds a sub-section to clause 17 specifying the 
semantics implied by an argument specified as <kbd>error_code&amp; ec=</kbd><i><kbd>unspecified-name</kbd></i>.</p>
<p>The original plan was to hold this proposal for TR2, where libraries like 
filesystem will make extensive use of this error handling idiom. A C++0x use (LWG 
issue 935, <i>clock error handling needs to be specified</i>) has now 
arisen, so it may be desirable to add it for C++0x.</p>
<h2>Motivating example</h2>
<p>Each of the three clocks specified in 20.9.5 Clocks [time.clock] provides the 
member function:</p>
<blockquote>
  <pre>static time_point now();</pre>
</blockquote>
<p>The semantics are specified by 20.9.1 Clock requirements [time.clock.req], 
which as of CD1 makes no mention of error handling. Thus the function may throw
<kbd>bad_alloc</kbd> or an implementation-defined exception (see [res.on.exception.handling] 
paragraph 4). Since the function is sometimes used in cases where exceptions are 
not appropriate or where the specifics of the exception or cause of error need 
to be available to the user, further options are needed.  </p>
<p>Indeed, this is an example of a class of errors where use cases require that three 
possible semantic actions be supported:</p>
<ul>
  <li>Throw an exception of of type <code>system_error</code> or of a type derived from <code>system_error</code>.</li>
</ul>
<blockquote>
  <blockquote>
    <p>This is the most appropriate default behavior, since it covers the very 
    common use case where errors are not expected and the user does not wish to 
    or have the knowledge to code anything dealing with error handling. Because 
    the exception hierarchy is specified to derive from <kbd>system_error</kbd>, 
    information about the cause of an error is provided should an exception 
    occur.</p>
    <p>This semantic action needs to be provided by <code>now()</code> because 
    it is often used in cases where errors are truly 
    exceptional and if they do occur are best handled further up the call chain. 
    Thus this option does need to be available for clock users and is the 
    appropriate default.</p>
  </blockquote>
</blockquote>
<ul>
  <li>Report the error via an object of type <kbd>error_code</kbd>.</li>
</ul>
<blockquote>
  <blockquote>
    <p>This allows users to process errors, including details, yet does not 
    incur the runtime and code complexity cost of try/catch blocks. Without this 
    option, users report code becomes so littered with try/catch blocks that it 
    is unreadable and unmaintainable.</p>
    <p>For clocks, this option is particularly attractive for code that must be 
    robust across a wide variety of platforms, including embedded and legacy 
    systems.</p>
  </blockquote>
</blockquote>
<ul>
  <li>Ignore the error.</li>
</ul>
<blockquote>
  <blockquote>
    <p>This is just a subset of reporting via <kbd>error_code</kbd> object; the 
    object is simply ignored. It is particularly appropriate in destructors.</p>
    <p><code>now()</code> is often used in destructors, so having this semantic 
    action makes 
    life simpler for clock users.</p>
  </blockquote>
</blockquote>
<p>The proposed wording gives the user the choice of these three semantic 
actions on an error, yet does not require a doubling or tripling of the number 
of function signatures that must be specified in the standard or in actual 
interfaces. Applying this to the 
clock example, the <code>now</code> function specification becomes:</p>
<blockquote>
  <p><kbd>static time_point now(error_code&amp; ec=<i>unspecified-name</i>);</kbd></p>
</blockquote>
<p>Implementations are permitted to actually implement this as one or two 
signatures.</p>
<h2>Implementation experience</h2>
<p>The requisite extern was added to Boost at release 1.37.0. The idiom had been 
in more limited use approximately six months and is the outgrowth of several 
years of experience with predecessor idioms.</p>

<h2>Proposed wording</h2>
  <p><i>After 17.6.5.12 Value of error codes [value.error.codes], add a new 
  subsection:</i></p>
    <blockquote>
  <p><b>17.6.5.13&nbsp; Semantics of <code>error_code&amp;</code><kbd> </kbd></b> <code>
  <b><kbd>ec=</kbd></b><i><b><kbd>unspecified-name</kbd></b> </i></code><b>arguments&nbsp; [semantics.throws]</b></p>
  <p>Certain standard library functions are specified as having an argument of <code>error_code&amp; 
  ec=<i>unspecified-name</i></code>. <code><i>unspecified-name</i></code> shall 
  be a name reserved for the implementation ([reserved.names]).&nbsp;&nbsp; <i>
  [Note:</i>
  <i> <code>unspecified-name</code></i> serves only to denote a request for the 
  default error handling behavior.&nbsp; <i> --end note]</i></p>
  <p>Such functions shall behave as if specified as two separate signatures:</p>
      <blockquote>
  <p>A function signature without an <code>error_code&amp;</code> argument. If an error 
  occurs, this function shall throw an exception of type <code>system_error</code>, or of a type derived from <code>system_error</code>, 
  whose <kbd>code()</kbd> member function shall identify the reason for the 
  error.</p>
  <p>A function signature with an <code>error_code&amp; 
  ec</code> argument. This function shall not throw exceptions. If an error 
  occurs, this function shall have an implicit postcondition that <kbd>
  ec.value()</kbd>and <kbd>ec.category()</kbd> identify the reason for the 
  error. If an error does not occur, this functions shall have an implicit 
  postcondition that <kbd>!ec</kbd> is true. Unless specified, the 
  return value if an error occurs is unspecified.</p>
      </blockquote>
  <p>Whether or not implementations actually replace a single signature with a 
  defaulted <code>error_code&amp; 
  ec</code> argument with two signatures without defaulted <code>error_code&amp; 
  ec</code> arguments is unspecified.</p>
</blockquote>

<h2>Acknowledgements</h2>
<p>Howard Hinnant suggested use of an unspecified name rather than a specific name to denote the default 
case, and provided other helpful suggestions and comments that have been 
incorporated into this proposal. </p>

<hr>

<p>Revised
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%B %d, %Y" startspan -->December 05, 2008<!--webbot bot="Timestamp" endspan i-checksum="39486" --></p>

<p> Copyright Beman Dawes, 2008</p>

<p>Distributed under the Boost Software License, Version 1.0.
(See file <a href="../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a>
or&nbsp; <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>) </p>

</body>

</html>