
This course includes our updated coding exercises so you can practice your skills as you learn.
See a demo
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.
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.
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.
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.