Exceptionally Bad: The Misuse of Exceptions in C++ & How to Do Better - Peter Muldoon - CppCon 2023

Don't misuse exceptions in C++: learn how to use them correctly for serious errors, and discover why exceptions in interfaces and rethrowing without context are discouraged, along with alternative error handling methods.

Key takeaways
  • Exception classes are used incorrectly to control flow and for non-useful information.
  • Incorrect usage leads to exceptions in interfaces, which results in a corrosive effect on interface design.
  • Using exceptions for trivial things like return codes can lead to their misuse.
  • Exceptions should be used for serious, infrequent, and unexpected errors.
  • Exception handling can be slow and expensive, especially with complex hierarchies and multiple inheritance.
  • Rethrowing exceptions without adding context is discouraged, as it does not allow for better error investigation.
  • A single catch handler can handle multiple exceptions, making it more efficient.
  • Exceptions can transfer information up the stack to the handler, but this is more expensive than other methods.
  • The recommended way of error handling is using return codes and logging, not exceptions.
  • Using exceptions should be kept to a minimum and only for serious errors.
  • There is no formal definition of what an exception class is, making it difficult to determine what distinguishes an exception class.
  • Exception handling should be done locally, not in the try-catch block.
  • Learned behavior should be incorporated into exception handling design.
  • Exception classes should not be used for counting, as this is not their intended purpose.
  • Everything thrown is an exception, but not everything is an exception class.
  • Exception handling is not guaranteed to be caught, and if not caught, the program may terminate.