Exceptions
throw an exception and catch it with try catch. Catch by const reference.
Example
#include <iostream>
#include <stdexcept>
int main() {
try {
throw std::runtime_error("boom");
} catch (const std::exception &e) {
std::cout << "caught " << e.what() << "\n";
}
return 0;
}Result
caught boom
Exercise
Catch block.
(const std::exception &e)Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.