
This course includes our updated coding exercises so you can practice your skills as you learn.
See a demo
Design functions that normally return a value but may fail, choosing safe signatures to prevent misuse. Explore error reporting principles with file-reading examples and [[nodiscard]] in C++17 and beyond.
Develop practical error reporting skills in C++ by completing coding exercises that require modifying run_test function to handle readFile output, check errors, and report results with tester.reportResult.
Explore how exceptions report errors by exiting scopes and unwinding the stack to keep APIs simple and focus on the happy path, with custom exception types and strategic catching.
Explore reporting errors with empty values by returning an empty or default value on error and the actual value otherwise, noting that this simple approach cannot distinguish errors.
Explore reporting errors by returning invalid values. Distinguish between different kinds of errors, while noting the risks of using the same type for both contents and errors.
Learn how std::optional, introduced in C++17, reports errors by returning an empty optional on failure, forcing callers to check for a value before accessing contents.
Use booleans to report file read success or failure, with true signaling success and false signaling error. They cannot distinguish error types and require content as an out-parameter.
Explore how error codes—integers in C and C++—report different errors, enable cross-process communication and bitwise operations, and reveal payload handling and protection flaws shown by readFile and switch use.
Return a boolean and the file contents as a single pair to signal errors and supply the payload as a constant, while noting you cannot distinguish error types.
Return a pair of an error code and the file contents to report errors, enabling differentiation of error types and access to contents as a constant, while noting potential misuse.
Compare a custom class that holds a boolean and file content with the pair approach, using success and content members, and note its expressive benefits and the same drawbacks.
Compare this final alternative to the pair approach by returning a custom class that holds an error code and file contents, offering clearer readability and differentiated errors.
Explore std::variant as a type-safe union that may hold either an error code or file contents. Use std::holds_alternative and std::get to handle errors and access contents safely.
explains std::expected, a type that holds two types for error reporting in C++, showing expected and unexpected types, has_value, value, error, and monadic operations to build readable pipelines.
Explore error reporting strategies in C++, balancing exceptions with alternative error handling, and choose appropriate return types such as boolean, error codes, std::optional, and std::expected depending on payload.
Let's explore how to report errors in C++ for software that's both expressive and hard to misuse.
In this course, you will explore various error reporting mechanisms and essential best practices for creating robust and reliable software.
Error reporting is a crucial part of software development. It increases the correctness of software and makes it more maintainable. The goal of error reporting is simple: When returning from a function that may fail, make correct code easy to write and incorrect code hard to miss.
We will look at different ways to report errors in C++ and evaluate each technique based on its simplicity, expressiveness, and robustness. The techniques we will examine range from the most basic, essentially writing C, to the most modern, using C++23 features. The focus will be on functions that return some value, such as the contents of a file, and may fail.
The challenge is finding an ideal way to report an error if one occurs, while also returning the value if everything goes well.
Errors should be unignorable and unambiguous. If a function returns a value, we want to ensure that the caller cannot accidentally access that value if an error has occurred. Additionally, we would like the caller to know what went wrong in case of a mishap. All this, expressively and simply.
We will look at different ways to achieve this, from using exceptions to returning empty or invalid values, error codes, and more. The lectures are code-centric and concise. They are accompanied by interactive labs to help you practice what you have learned. In the coding exercises, you will get some starter code, and your task is to handle the output of a function that may fail. This will allow you to practice all the different error reporting techniques while exploring their advantages and disadvantages.
Join me in this course to learn how to report errors by writing code that's hard to misuse and wrong code hard to get away with.