Mentions légales du service

Skip to content
Snippets Groups Projects

Fix following training session from the 20th of May 2021

1 unresolved thread
3 files
+ 142
64
Compare changes
  • Side-by-side
  • Inline
Files
3
catch(int n)
catch(int n)
{
{
if (n == 1)
if (n == 1)
std::cout << "Error: value is bigger than 9!" << std::endl;
std::cerr << "Error: value is bigger than 9!" << std::endl;
if (n == -1)
if (n == -1)
std::cout << "Error: value is less than -9!" << std::endl;
std::cerr << "Error: value is less than -9!" << std::endl;
}
}
std::cout << "End" << std::endl;
std::cout << "End" << std::endl;
}
}
}
}
catch(float n) // doesn't catch your `int` exception!
catch(float n) // doesn't catch your `int` exception!
{
{
std::cout << "Float case: " << n << " was provided and is not an integer" << std::endl;
std::cerr << "Float case: " << n << " was provided and is not an integer" << std::endl;
}
}
catch(...)
catch(...)
{
{
std::cout << "Gluttony case... but no object to manipulate to extract more information!" << std::endl;
std::cerr << "Gluttony case... but no object to manipulate to extract more information!" << std::endl;
}
}
}
}
}
}
catch(int n)
catch(int n)
{
{
std::cout << "Int case: " << n << " not a single digit number" << std::endl;
std::cerr << "Int case: " << n << " not a single digit number" << std::endl;
}
}
std::cout << "After catch" << std::endl;
std::cout << "After catch" << std::endl;
}
}
catch(int n)
catch(int n)
{
{
std::cout << "Int case: " << n << " not a single digit number" << std::endl;
std::cerr << "Int case: " << n << " not a single digit number" << std::endl;
throw; // `throw n` would have been correct as well but is not necessary
throw; // `throw n` would have been correct as well but is not necessary
}
}
}
}
catch(const std::exception& e)
catch(const std::exception& e)
{
{
std::cout << "Properly caught: " << e.what() << std::endl;
std::cerr << "Properly caught: " << e.what() << std::endl;
}
}
}
}
with the latter being the default choice if no other fit your issue. Most of those classes provide a `std::string` argument in its constructor so that you may explain exactly what went wrong.
with the latter being the default choice if no other fit your issue. Most of those classes provide a `std::string` argument in its constructor so that you may explain exactly what went wrong.
 
The previous case could be rewritten as:
 
%% Cell type:code id: tags:
 
``` C++17
 
#include <iostream>
 
#include <exception>
 
 
void FunctionThatExpectsSingleDigitNumber3(int n)
 
{
 
if (n < -9)
 
throw std::out_of_range("Value is less than -9!");
 
 
if (n > 9)
 
throw std::out_of_range("Value is more than 9!");
 
 
std::cout << "Valid digit is " << n << std::endl;
 
}
 
%% Cell type:code id: tags:
 
``` C++17
 
FunctionThatExpectsSingleDigitNumber3(-10);
 
%% Cell type:code id: tags:
 
``` C++17
 
FunctionThatExpectsSingleDigitNumber3(3);
 
%% Cell type:code id: tags:
 
``` C++17
 
FunctionThatExpectsSingleDigitNumber3(15);
 
%% Cell type:markdown id: tags:
### `noexcept`
### `noexcept`
Exceptions are in fact very subtle to use; see for instance \cite{Sutter1999} and \cite{Sutter2002} that deal with them extensively (hence their title!).
Exceptions are in fact very subtle to use; see for instance \cite{Sutter1999} and \cite{Sutter2002} that deal with them extensively (hence their title!).
The explanation is quite subtle and explained in detail in item 8 of \cite{Meyers2005}; however just know you should never throw an exception there. If you need to deal with an error there, use something else (`std::abort` for instance).
The explanation is quite subtle and explained in detail in item 8 of \cite{Meyers2005}; however just know you should never throw an exception there. If you need to deal with an error there, use something else (`std::abort` for instance).
### The exception class I use
I (Sébastien) provide in [appendix](/notebooks/7-Appendix/HomemadeException.ipynb) my own exception class (which of course derives from `std::exception`) which provides additionally:
* A constructor with a string, to avoid defining a verbosy dedicated exception class for each case.
* Better management of the string display, with an underlying `std::string` object.
* Information about the location from where the exception was thrown.
Vincent uses up the STL exceptions described in [previous section](#Good-practice:-be-wary-of-a-forest-of-exception-classes).
## Error codes
## Error codes
A quick word about C-style error management which you might find in use in some libraries: **error codes**.
A quick word about C-style error management which you might find in use in some libraries: **error codes**.
Loading