Exception-Safe Coding in C++
Are you 100% confident that your code is exception-safe?
Safe usage of exceptions is a non-trivial problem that the industry has struggled with for the better part of two decades. If you have fear, uncertainty, or doubt about exception safety or just want to see the best practices for using exceptions in C++, this session is for you. We’ll start with “What is the problem we are trying to solve?” and discuss alternatives, acknowledge the challenges associated with exception usage, and cover some well-meaning but misguided attempts at safety. I will then present a set of guidelines that are the basis for safe exception usage and solid implementation techniques, including how to transition from an exception-unsafe legacy code base.
When we are finished you will know how to produce code that is easier to write, easier to understand, faster, and 100% robust in the face of exceptions.
I was invited to the be the first guest on Rob Irving's new C++ podcast, CppCast.
Please show your support for CppCast by subscribing:
I'm now actively soliciting onsite training engagements. For information on course content, dates, and rates, please contact me.
I've given the Exception Safety talk again at the 2014 Silicon Valley Code Camp on October 11th as part of the C++ Track.
I've given the talk as a three parter at the CppCon on September 9th. The talk was recorded and the recordings are available as part the CppCon YouTube Channel.
I've given the talk again at the 2013 Silicon Valley Code Camp on October 5th as part of the C++ Track.
It was a two-parter, just like at C++ Now!.
I gave the talk at Silicon Valley ACCU on January 23, and February 27th, 2013.
I gave the talk at the 2012 Silicon Valley Code Camp on October 6th.
Are exceptions that throw evil?. That is what I've always said in my talks, but Dave Abrahams and I have explored this idea in a recent blog post on C++ Next.
The video of my BoostCon 2012 talk is now available in the BoostCon channel on YouTube and I have embedded both parts below.
My talk at the 2012 C++ Now! Conference (better known as BoostCon) won Best Tutorial.
This talk was a longer version (three hours in two parts) so I was able to cover some of the material that I removed in order to (almost) fit into the Code Camp seventy-five minute restriction. This additional time also allowed me to update my talk for the exceptions-related changes in the new C++11 Standard.
For the first time, I'm making my slides available.
Handouts
Video
BoostCon 2012 version (updated for C++11) Part 1 of 2 (90 min):
BoostCon 2012 version (updated for C++11) Part 2 of 2 (90 min):
An additional comment: (refresh)
Jon's coverage of exception-safe C++ code is top-notch! I had traditionally avoided C++ exceptions in my code, scared off by concerns of resource leaks and data corruption. This resulted in messy code that was littered with status checks. As Jon correctly points out, exception-safe code is generally shorter and easier to write. Furthermore, the resulting code is actually -safer- with regard to leaks and data corruption. I'll never think twice about using C++ exceptions again.
--