1
Exception Handling
C++
Errors are thrown with throw and caught with try / catch, so the program does not crash.
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();
}