Workshop: C++ Exception Handling in Detail
Used effectively, the C++ type system can turn many potential run-time errors into compile-time errors. However, there are some errors that just can’t be caught until run time. A program that compiles cleanly may still find that objects have invalid values, that files are corrupted, or that memory or other resources are unavailable.
Exception handling can make it easier to detect and handle run-time errors in a robust fashion. One of the main challenges to writing robust error reporting systems is that the code that detects an error is often far removed from the code that handles it. Exception handling simplifies this by transferring control and information directly from the detector to the handler with minimal impact on the intervening code.
This course explains how to use C++ exceptions to report errors cleanly, concisely, and reliably. This course demonstrates how handling errors by throwing and catching exceptions can simplify normal control flow. It presents best practices on how and when to throw exceptions, and how and when to catch them. It also shows how to avoid common problems that arise when using exceptions. Understanding exception handling and its associated techniques will help you create components that are simpler and easier to use, yet more robust at detecting and handling errors.
• Drawbacks of legacy error reporting techniques
• Throwing and catching exceptions
• Stack unwinding and destructors
• Resource Acquisition Is Initialization (RAII)
• Constructors that throw
• Why destructors shouldn’t throw
• Exception safety guarantees
• The Copy-Swap Idiom
• noexcept and exception specifications
Attendees should have basic knowledge of C++ classes and members, constructors, destructors, inheritance, namespaces, new-expressions, and references.
Attendees should be set up to edit, compile, and run C++14 command-line applications. The exercises include makefiles for multiple compilers, including GCC and Microsoft Visual Studio.