Back In the print window choose "Save as PDF" as the destination
NOTE.CS

Exception Handling

1 topic ยท 28 Nov 2026
1

Exception Handling

C++

Errors are thrown with throw and caught with try / catch, so the program does not crash.

C++
int divide(int a, int b) {
    if (b == 0) throw runtime_error("Division by zero");
    return a / b;
}

try {
    cout << divide(10, 0);
} catch (const exception &e) {
    cout << "Error: " << e.what();
}