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.
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.
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.
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:
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.