You know that old saying about not using exceptions to govern control flow… I never really felt it was that big a deal. If you use an exception for validation, for example, it seems like fair game. The validation asserts that things are correct and then throws if they’re not. No problem, right?
It turns out, from a recent profiling activity, that a bit of code I’ve been performance tuning spends 28% of its time throwing exceptions… That’s rather a lot.
When we throw an exception, we have to construct the call stack that describes what happened, and this is slightly time consuming. When it’s in the middle of a 10,000 item loop, which is testing to see which items match and which don’t, using a validation-style function call with a
catch block to say not this one…
… well, it turns out that that’s really a rather bad way of doing things!
Who would have thought it!?