Choose Your Poison: Error Handling using Exceptions vs. Error Codes

Highlights

  • Thorough overview of exception motivation and mechanics
  • Simple prescription for robust error handling in the case of multiple subsystem failure
  • Reintroduces the now famous ScopeGuard abstraction in a simpler and better form using C++2011 lambda functions
  • Introduces a method for packing exceptions in a simple abstraction

Attendee profile

C++ knowledge is assumed. Understanding of exception handling mechanisms is recommended but not necessary. This class is best suited for library designers and system architects.

Outline

  • Coding with exceptions: overview
    • Motivation
    • Mechanics
    • Comparison and contrast with alternative error handling means
    • Dos and donts
      • Never catch by value
      • Never throw int or string
      • Unlearn SESE
      • Either succeed of fail successfully
      • Avoid converting exceptions to error codes
      • Avoid all throw specs
      • Prefer alternatives to catch/rethrow
  • Efficiency impact of exceptions
  • Exceptions vs. assert vs. core dumps
  • Throw/catch ratio
  • Transactional code
    • Sample problem: a function with two failure points
    • Solution 1: Brute force
    • Solution 2: Politically correct
    • Solution 3: Real-world
    • Solution 4: Using a local struct
    • Solution 5: Using ScopeGuard
    • ScopeGuard and C++2011
    • Implementing ScopeGuard with lambda functions
  • Packing exceptions using the Likely template
    • Type erasure
    • The None type
    • Defining Likely
      • Overt path
      • Covert path
  • Summary