Chapter 2. Returning Error Information
The previous chapter focused on error handling. This chapter continues this discussion, but focuses on how to inform users of your code about the errors detected.
For every larger program, programmers have to decide how to react to errors arising in their own code, how to react to errors arising in third-party code, how to pass this error information along in the code, and how to present this error information to the user.
Most object-oriented programming languages come with the handy mechanism of exceptions to provide the programmer with an additional channel for returning error information, but C does not natively provide such a mechanism. There are ways to emulate exception handling or even inheritance among exceptions in C, for example as described in the book Object-Oriented Programming with ANSI-C by Axel-Tobias Schreiner (2011). But for C programmers working on legacy C code or for C programmers who want to stick to the native C style they are used to, introducing such exception mechanisms is not the way to go. Instead, such C programmers need guidance on how to use the mechanisms for error handling already natively present in C.
This chapter provides such guidance on how error information can be transported between functions and across interfaces. Figure 2-1 shows an overview of the patterns covered in this chapter and their relationships, and Table 2-1 provides a summary of the patterns.
Get Fluent C now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.