|
| 1 | +## Error handling: Error codes {#ecodes} |
| 2 | + |
| 3 | +_Skeleton descriptions are typeset in italic text,_ |
| 4 | +_so please don't remove these descriptions when editing the topic._ |
| 5 | + |
| 6 | +### Overview |
| 7 | + |
| 8 | +_Provides a short natural language abstract of the module’s contents._ |
| 9 | +_Specifies the different levels of teaching._ |
| 10 | + |
| 11 | +------------------------------------------------------------------------ |
| 12 | +Level Objective |
| 13 | +----------------- ------------------------------------------------------ |
| 14 | +Foundational Handling error codes with `std::error_code` |
| 15 | + |
| 16 | +Main Designing APIs around `std::error_code` |
| 17 | + |
| 18 | +Advanced --- |
| 19 | + |
| 20 | +------------------------------------------------------------------------ |
| 21 | + |
| 22 | +### Motivation |
| 23 | + |
| 24 | +_Why is this important?_ |
| 25 | +_Why do we want to learn/teach this topic?_ |
| 26 | + |
| 27 | +C++ offers a type safe way of passing around errors, contrary to the C-style of error handling, by this, we prevent bugs when passing error codes. |
| 28 | +Furthermore, error handling with error codes is more commonly used than exception handling, which only should be used in exceptional situations and in some environments is not feasible at all, e.g., in embedded or performance critical software. |
| 29 | + |
| 30 | +### Topic introduction |
| 31 | + |
| 32 | +_Very brief introduction to the topic._ |
| 33 | + |
| 34 | +C++ offers `std::error_code`, which encapsulates error codes in a type safe way. |
| 35 | +This topic describes how to use these error codes. |
| 36 | + |
| 37 | +### Foundational: Handling error codes with `std::error_code` {#eh-found} |
| 38 | + |
| 39 | +#### Background/Required Knowledge |
| 40 | + |
| 41 | +A student: |
| 42 | + |
| 43 | +* should know that there are different ways of error handling [[Error handling: Categories of errors]][1] |
| 44 | +* should know function return values |
| 45 | + |
| 46 | +#### Student outcomes |
| 47 | + |
| 48 | +_A list of things "a student should be able to" after the curriculum._ |
| 49 | +_The next word should be an action word and testable in an exam._ |
| 50 | +_Max 5 items._ |
| 51 | + |
| 52 | +A student should be able to: |
| 53 | + |
| 54 | +1. write code to handle errors with `std::error_code`, e.g., obtain the message of the error code or check if an error occurred. |
| 55 | +2. distinguish between the different categories and make justified decisions when to use which |
| 56 | + |
| 57 | +#### Caveats |
| 58 | + |
| 59 | +_This section mentions subtle points to understand, like anything resulting in |
| 60 | +implementation-defined, unspecified, or undefined behavior._ |
| 61 | + |
| 62 | +#### Points to cover |
| 63 | + |
| 64 | +_This section lists important details for each point._ |
| 65 | + |
| 66 | +* a brief overview of `std::error_code` and how to use it |
| 67 | + |
| 68 | +### Main: Designing APIs around `std::error_code` {#eh-main} |
| 69 | + |
| 70 | +#### Background/Required Knowledge |
| 71 | + |
| 72 | +* should know how to use reference parameters as an output parameter |
| 73 | + |
| 74 | +#### Student outcomes |
| 75 | + |
| 76 | +A student should be able to: |
| 77 | + |
| 78 | +1. create an `error_code` and design API that work with `std:error_code` |
| 79 | +2. write code that utilizes `std::error_category` |
| 80 | +3. explain the difference between C-style error handling with errno and `std::error_code` |
| 81 | +4. make effective use of the interface of `std::error_code` |
| 82 | + |
| 83 | +#### Caveats |
| 84 | + |
| 85 | +* reset errno before calling a function that might set errno (better pass an input parameter `std::error_code`) |
| 86 | + |
| 87 | +#### Points to cover |
| 88 | + |
| 89 | +* provide a full picture of `std::error_code` and it’s APIs |
| 90 | +* `std::error_category` (explorative) |
| 91 | + |
| 92 | +### Advanced |
| 93 | + |
| 94 | +_These are important topics that are not expected to be covered but provide |
| 95 | +guidance where one can continue to investigate this topic in more depth._ |
| 96 | + |
| 97 | +* implementing your own `error_category` |
| 98 | + |
| 99 | +[1]: error-handling/categories-of-errors.md |
0 commit comments