# Exceptions ___ ## Throw If you want to throw an exception you can simply call `throw` ```C++ void foo() { throw "Error"; } ``` If you want to catch an error you need to use `try-catch` ```C++ void bar() { try { foo(); } catch (const std::exception& err) { std::cout << err.what() << '\n'; } } ``` ___ ## What happens during throw? Programm will stop executing the next line, when reach `throw` command. It will startup **unwind** a stack untill it reach the first `try-catch` block which will catch this exception. Because the stack is unwinded all variables which go out of scope will be properly destructed.
enum class ErrorCode
* return nullptr
* return false
* return std::optional (C++17)
* return std::expected (C++23)
___
## Descriptive errors
We have three ways to strictly describe what happens wrong: `exception`, `enum class`, and `std::expected`. Using these types we can say what exactly goes wrong, like: providing the wrong credentials, or we can't find some element, etc...