10

I don't need a lesson in switching from recursive to non-recursive means, I just want to know why we can't deal with this type of exception. Regardless, I'm using recursive functions on very large lists.

I have written code to attempt to catch StackOverFlowExceptions:

try { recursiveFxn(100000); }
catch(Exception){}
private void recursiveFxn(int countdown)
{
if (countdown > 0)
    recursiveFxn(countdown - 1);
else
    throw new Exception("lol.  Forced exception.");
}

But still I get program crashes (in both NUnit and a webpage I'm running). Why isn't the exception caught?

2
  • i'm kinda surprised this exception exists... outside of managed code, i'm not convinced it's possible to recover from this error in general. Commented Aug 26, 2010 at 0:40
  • @RobertKarl: I wish there was a means via which code could explicitly check the stack for a certain amount of space, with an exception being thrown if the space wasn't available. If such a method threw the exception before the stack overflowed, and if the amount of space requested was at least equal to the sum of the maximum about of stack that will be allocated between tests, plus the amount required for recovery code, such exceptions could be 100% recoverable. As it is, I know of no way to write safe recursive code which does not artificially limit the depth of structures it can handle. Commented Jan 7, 2013 at 18:17

2 Answers 2

16

Since .NET Framework 2.0, StackOverflowException cannot be caught. This is because it is considered a bad practice. Quoting the MSDN documentation:

Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow. For example, if your application depends on recursion, use a counter or a state condition to terminate the recursive loop.

Now, the only way to catch a StackOverflowException is when it was thrown by user code, as explained in a blog by Jared Parsons. Other than that, by hosting the CLR, you can handle (but not catch) a StackOverflowException and devise a way to let the execution of your program continue.

Note that because the stack is unwound when an exception occurs, in pre-2.0 versions of .Net the stack would actually be much shorter when the StackOverflowException is handled, making it possible to do so without generating another StackOverflowException.

Sign up to request clarification or add additional context in comments.

Comments

-1

You can't catch a stack overflow exception because when it happens it kills the thread dead. Try... catch... is performed by the same thread so that won't work. There may be some lower level APIs that you could P/Invoke and have another thread catch it. There may also be some lower level APIs to change the maximum stack size, but I don't see anything in the .NET Framework to help with that so again you would need to P/Invoke something.

2 Comments

-1 because as I note above, stack overflow exceptions could be caught in previous versions of .Net, so your answer that it 'won't work because it is on the same thread' is wrong.
@Virtlink: If I recall correctly, in .net 1.0, if tried to catch StackOverflowException, it would "usually" work. There was a substantial overhead, however, to allowing such exceptions to "usually" be caught before the thread context was damaged beyond repair, and despite such overhead the feature wasn't reliable enough to be useful. In .net 2.0, Microsoft decided that it was better to require that programmers use some other means of recovering from such conditions, than to incur the overhead so they could try to use a recovery approach that may not work anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.