Handling Errors with C++
This chapter will focus on error handling in C++. As a programmer, you will inevitably encounter situations where you need to determine the best approach to propagate program errors. Whether you use error codes or exceptions, we will delve into them to gain a better understanding of how to use them effectively.
In this chapter, we will examine how to handle errors reported by POSIX APIs using C++. We will begin by covering the errno
thread-local variable and the strerror
function. After that, we will introduce std::error_code
and std::error_condition
and demonstrate how they help to wrap POSIX errors that come from POSIX APIs. We will also investigate custom error categories, which allow us to compare errors produced by various sources and develop platform-independent error-handling code.
As we progress, we will learn about exceptions in C++ and how to convert std::error_code
into a std::system_error
exception. We will also explore some best practices...