.NET › CLR & Runtime
How does the CLR handle exceptions at a low level, and what is the performance implication of throwing exceptions frequently?
AdvancedAnswer
When an exception is thrown, the CLR walks up the call stack searching for a matching catch handler, unwinding stack frames and running finally blocks along the way -- this stack-walking and unwinding process is comparatively expensive (orders of magnitude slower than a normal method return), which is why exceptions should be reserved for genuinely exceptional conditions rather than used for routine control flow, where return values or the Try-pattern (TryParse, TryGetValue) are much more performant alternatives.
// Expensive: using exceptions for expected, frequent control flow
try { return int.Parse(userInput); }
catch (FormatException) { return 0; } // exception thrown on every invalid input
// Better: TryParse avoids exception overhead entirely
return int.TryParse(userInput, out var result) ? result : 0;Real-world example A high-throughput data validation pipeline processing millions of potentially malformed records switches from try/catch-based parsing to TryParse-based validation, measurably improving throughput since exceptions were previously being thrown on a significant fraction of malformed records.