Channels ▼

Walter Bright

Dr. Dobb's Bloggers

Nothrow Functions

September 28, 2008

In my last installment, I covered pure functions in the D programming language. Also new for D 2.0 is the notion of nothrow functions.

A nothrow function is a function that is guaranteed to not throw any exceptions. It must complete (or abort the program). The nothrow condition is statically checked by the compiler:

    void bar();
    nothrow void abc();

    nothrow void foo()
    {
        bar();                  // error, bar() may throw
        abc();                  // ok, abc() does not throw
        throw new Exception();  // error, cannot throw
    }

A function whose internals throw exceptions can be converted to nothrow by wrapping the throwing code into an exception handler:

    nothrow void foo()
    {
       try
      {
          ... code that may throw ...
      }
      catch (Object o)
      {
         ... handle any exceptions ...
      }
      ... now we're nothrow ...
    }

Nothrow functions offer the highest level of exception safety (1). Allowing functions to be annotated with the nothrow attribute enables the compiler to statically enforce it.

As with pure functions, the most obvious advantage of nothrow functions is the self documenting aspect of it. Since the compiler guarantees that a nothrow function really doesn't throw, the programmer need look no further to verify it. This is especially convenient when the source for the function isn't available, like in an API for a library.

Nothrow is needed for move and swap functions, because for transactional programming all the work is done on the side, and then the result is move()d into the target. If such a move could throw an exception, this could not be made correct.

Declaring a function nothrow enables the compiler to omit the default behavior
of generating exception hndlers to unwind objects just in case the function throws.
For example,

    void foo();

    void abc()
    {  scope (exit) bar();
       foo();
    }

causes the compiler to generate the equivalent of:

    void abc()
    {  try
       {
         foo();
       }
       finally
       {
         bar();
       }
    }

But if foo() were declared nothrow, the simpler form:

    nothrow void foo();

    void abc()
    {
         foo();
         bar();
    }

can be generated.

In conclusion, nothrow functions have a compelling case for them made up of their inherently self-documenting nature, the highest exception safety evel, their utility in writing correct transactional code, and their usefulness in enabling the compiler to generate better code.


References:

(1) "Exception-Safety in Generic Components" by David Abrahams
http://www.boost.org/community/exception_safety.html

Acknowledgements:

Thanks to Andrei Alexandrescu and Bartosz Milewski for reviewing this.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video