C++ lets you catch and handle runtime errors safely using try
, throw
, and catch
blocks — so your app doesn't crash on bad input or logic errors.
try { /* risky code */ }
— wrap any code that might failthrow
— raises an exception (can be a string, int, object, etc.)catch
— handles the thrown errorcatch
blocks can handle different exception typestry { throw 404; } catch (int code) { cout << "Error code: " << code; } catch (...) { cout << "Unknown error"; }