Modernes C++ programmieren

Okt 20, 2024

lst-0038-book.cpp

// 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 << " ";  // … handle it this way
    }
}