Fix following training session from the 20th of May 2021
1 unresolved thread
Compare changes
<div class="toc"><ul class="toc-item"><li><span><a href="#Introduction" data-toc-modified-id="Introduction-1">Introduction</a></span></li><li><span><a href="#Compiler-warnings-and-errors" data-toc-modified-id="Compiler-warnings-and-errors-2">Compiler warnings and errors</a></span></li><li><span><a href="#Assert" data-toc-modified-id="Assert-3">Assert</a></span></li><li><span><a href="#Exceptions" data-toc-modified-id="Exceptions-4">Exceptions</a></span><ul class="toc-item"><li><span><a href="#throw" data-toc-modified-id="throw-4.1"><code>throw</code></a></span></li><li><span><a href="#try/catch" data-toc-modified-id="try/catch-4.2"><code>try</code>/<code>catch</code></a></span></li><li><span><a href="#Re-throw" data-toc-modified-id="Re-throw-4.3">Re-throw</a></span></li><li><span><a href="#Good-practice:-use-as-much-as-possible-exceptions-that-derive-from-std::exception" data-toc-modified-id="Good-practice:-use-as-much-as-possible-exceptions-that-derive-from-std::exception-4.4">Good practice: use as much as possible exceptions that derive from <code>std::exception</code></a></span></li><li><span><a href="#Good-practice:-be-wary-of-a-forest-of-exception-classes" data-toc-modified-id="Good-practice:-be-wary-of-a-forest-of-exception-classes-4.5">Good practice: be wary of a forest of exception classes</a></span></li><li><span><a href="#noexcept" data-toc-modified-id="noexcept-4.6"><code>noexcept</code></a></span></li><li><span><a href="#Good-practice:-never-throw-an-exception-from-a-destructor" data-toc-modified-id="Good-practice:-never-throw-an-exception-from-a-destructor-4.7">Good practice: never throw an exception from a destructor</a></span></li><li><span><a href="#The-exception-class-I-use" data-toc-modified-id="The-exception-class-I-use-4.8">The exception class I use</a></span></li></ul></li><li><span><a href="#Error-codes" data-toc-modified-id="Error-codes-5">Error codes</a></span><ul class="toc-item"><li><span><a href="#nodiscard" data-toc-modified-id="nodiscard-5.1">nodiscard</a></span></li></ul></li></ul></div>
* Strive to make your code without any compiler warning: if there are even as less as 10 warnings, you might not see an eleventh that might sneak its way at some point. Activate as many types of warnings as possible for your compiler, and deactivate those unwanted with care (see [this notebook](../6-InRealEnvironment/4-ThirdParty.ipynb) to see how to manage third party warnings.).
```
```
```
```
```
```
```
```
```
```
```
```
Unfortunately, you are not always privy to the choice: if for instance you're using [Boost library](https://www.boost.org) the exception class they use don't inherit from `std::exception` (but some derived ones such as `boost::filesystem::error` do...). In this case, make sure to foresee to catch them with a dedicated block:
```
STL provides many derived class from `std::exception` which you might use directly or as base of your own class; see [cppreference](https://en.cppreference.com/w/cpp/error/exception) for more details. [OpenClassrooms](https://openclassrooms.com/fr/courses/1894236-programmez-avec-le-langage-c/1903837-gerez-des-erreurs-avec-les-exceptions) (which despite its name is in french) sorted out the go-to exceptions for lazy developers which cover most of the cases (don't get me wrong: laziness is often an asset for a software developer!):
```
```
```
```
In short, if you have a method you're 100 % percent sure can't throw an exception, add this suffix and the compiler may optimize even further. However, do not put it if an exception can be thrown: it would result in a ugly runtime crash should an exception be raised there... (and up to now compilers are completely oblivious to that: no associated warning is displayed).
```
```
```
```
```