17 The exceptional coder: Exception handling, logging, and QA

 

This chapter covers

  • Understanding the concept of exception handling
  • Learning how to throw and catch exceptions
  • Exploring exception safety guarantee and best practices
  • Unraveling the realm of logging

As you've learned so far, coding involves dealing with unexpected situations that can arise during program execution. Whether it's a file that's missing, a value that's out of range, or any number of unforeseen circumstances, C++'s exception handling mechanism provides an elegant solution to address these challenges.

We'll explore the fundamental concepts of exception handling, helping you understand how to gracefully manage errors and unexpected scenarios. We'll start by unraveling the basics, explaining what exceptions are, we'll guide you through the syntax and mechanics of throwing and catching exceptions. We'll provide real-world examples that illustrate the step-by-step process of raising exceptions when issues occur, and catching them to ensure your program continues executing smoothly. You'll gain insights into how to use various exception classes provided by the C++ Standard Library, each designed to cater to specific types of errors.

You will also learn about nested try-catch blocks, rethrowing exceptions, and utilizing the catch-all mechanism. You'll learn how to create custom exception classes tailored to your program's needs, enabling you to provide meaningful error messages and context to users.

17.1 Exception handling - Be an exceptional programmer

17.1.1 What to expect when you’re expecting (an exception)

17.1.2 Play catch: Throwing and Catching Exceptions

17.1.3 It’s classic: Exception handling and class objects

17.1.4 Catch ‘em all – The catch-all statement

17.2 Exception safety guarantee and best practices

17.2.1 Exception handling best practice

17.2.2 Final exercise: Alien Contact Exception

17.3 Bark up the right tree: The importance of logs

17.3.1 Principles of Effective Logging

17.3.2 The Regex vibe

17.3.3 Clear as a log: Writing your own logging mechanism

17.3.4 Enhancing Logging Flexibility: using Templated Functors and Lambdas

17.4 Log Utilities

17.5 Some Q&A about QA

17.6 summary