An exception does not automatically mean the program crashes. It only crashes if the exception is not caught.
In low-latency systems, exceptions are generally avoided because they:
-
Introduce latency overhead: when an exception is thrown, the runtime must unwind the stack i.e. walking back through active function calls and destroying local objects. This process takes time.
-
Create unpredictable control flow: exceptions can propagate across multiple layers before being caught, making the logic harder to follow.
So instead of doing this:
try {
place_order();
}
catch (const NetworkError& e) {
retry();
}
Prefer:
std::optional<OrderId> place_order(); // NOT a pointer
auto order_id = place_order();
if (!order_id) {
retry();
}