Besides the heap memory, there’s another finite resource in an application: its stack used to keep track of local variables, method calls, and evaluation of expressions. When this resource gets exhausted, a StackOverflowException results. Again, this is one of the exception types that’s uncatchable because properly dealing with such a circumstance is far from trivial. (You shouldn’t cause any further stack usage doing so.)

Typically, a StackOverflowException indicates an unbounded recursive algorithm in your code (which is a bug) or the fact you’re processing “deep” recursive data structures. Let’s take a look at a few examples:

void BlowUp() {    BlowUp();}

Depending on the amount of state kept for every frame on the call ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.