Nothrow Functions - InformationWeek
IoT
IoT
Government // Enterprise Architecture
Commentary
9/28/2008
12:00 AM
Commentary
Commentary
Commentary
50%
50%
RELATED EVENTS
Adopting Cloud the Right Way
Jun 14, 2017
Adopting a cloud mindset isn't about IT relinquishing all control and just approving invoices. It' ...Read More>>

Nothrow Functions

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.

Comment  | 
Print  | 
More Insights
Comments
Newest First  |  Oldest First  |  Threaded View
How Enterprises Are Attacking the IT Security Enterprise
How Enterprises Are Attacking the IT Security Enterprise
To learn more about what organizations are doing to tackle attacks and threats we surveyed a group of 300 IT and infosec professionals to find out what their biggest IT security challenges are and what they're doing to defend against today's threats. Download the report to see what they're saying.
Register for InformationWeek Newsletters
White Papers
Current Issue
2017 State of Data and Analytics
Today's companies are differentiating themselves using data analytics, but the journey requires adjustments to people, processes, technology, and culture. 
Video
Slideshows
Twitter Feed
InformationWeek Radio
Archived InformationWeek Radio
Join us for a roundup of the top stories on InformationWeek.com for the week of November 6, 2016. We'll be talking with the InformationWeek.com editors and correspondents who brought you the top stories of the week to get the "story behind the story."
Sponsored Live Streaming Video
Everything You've Been Told About Mobility Is Wrong
Attend this video symposium with Sean Wisdom, Global Director of Mobility Solutions, and learn about how you can harness powerful new products to mobilize your business potential.
Flash Poll