C++20 sections · 1024 units
Open in Course

Basic try-catch Example

First exception handler

Here's a complete example of exception handling:

try {
 int x = riskyOperation();
 cout << x;
}
catch (const runtime_error& e) {
 cout << "Error: " << e.what();
}
``` If an exception occurs, control jumps to the matching catch block. The catch parameter receives the exception object. After the catch block runs, execution continues after the try-catch.