[MECpp]Item-15 Understand the Costs of Exception Handling
· ☕ 2 min read
Exception handling has costs, and we pay at least some of them even if we never use the keywords try
, throw
, or catch
.
Exception handling has costs, and we pay at least some of them even if we never use the keywords try
, throw
, or catch
.
Exception specifications provide a documentation aid and an enforcement mechanism for constraints on exception usage, but they are only partly checked by compilers and they are easy to violate inadvertently.
If catching by reference, we sidestep the questions about object deletion, avoid slicing exception objects, retain the ability to catch standard exceptions, and limit the number of times exception objects being copied.
There are three primary ways in which passing an object to a function or using that object to invoke a virtual function differs from throwing the object as an exception.
Keep exceptions from propagating out of destructors to prevent C++ calling terminate
during the statck-unwinding part of exception propagation and ensure that every neccessary cleanup is accomplished.