June 2025
Intermediate to advanced
1093 pages
33h 24m
English
Exceptions are an important but advanced topic. Because the try with the corresponding catch is the only remaining statement, I will still give you a preview. I will go into detail in Chapter 10.
With a try block, you enclose program code where you expect certain errors that you want to handle. You handle these errors in the subsequent catch.
// https://godbolt.org/z/jhPaEjh1r#include <iostream>int main() { try { // beginning of the try block for(int n=1; ; n=n*2) { if (n > std::numeric_limits<int>::max()/2) { // check for coming overflow throw "There would be an overflow"; } } } // End of the try block catch(const char *error) { // if this error occurs, … std::cout << "Error: " << error << " "; // … ...
Read now
Unlock full access