3 ms·
Throwing exceptions is slow. Adding exception handling code itself has little to no overhead. Only throw exceptions in exceptional cases, not for frequently ex
by kaetemi 5y ago
Throwing exceptions is slow. Adding exception handling code itself has little to no overhead.
Only throw exceptions in exceptional cases, not for frequently expected outcomes.
- kaetemi 5y agoAdding to that, assume any C++ code can throw exceptions unless it's specifically stated it won't. If you're doing manual memory allocation in a function, your exception handling might be leaky. (gsl::finally is your friend.)
- renewiltord 5y agoJava exceptions build stacktrace at construct time, which makes for the fun fact that instantiating an exception is the majority of its cost. At least the last time I checked. Actually throwing it is some fifth of the cost. This is relevant in the log-and-throw-e situations.
- amelius 5y agoImagine you're writing a function that parses an integer from a string. Would you let it throw an exception on failure?
- kaetemi 5y agoDepends on whether you call the function `parse` or `tryParse`. :) Throw if you expect that the input should never be invalid. Don't throw, and do friendly logging and reporting of your parsing errors, if this is uncontrollable third party input.