Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How to test for StackOverflowException

Tags:

Say you have a method that could potentially get stuck in an endless method-call loop and crash with a StackOverflowException. For example my naive RecursiveSelect method mentioned in this question.

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.

Taking that information (from this answer) into account, since the exception can't be caught, is it even possible to write a test for something like this? Or would a test for this, if that failed, actually break the whole test-suite?

Note: I know I could just try it out and see what happens, but I am more interested in general information about it. Like, would different test frameworks and test runners handle this differently? Should I avoid a test like this even though it might be possible?

like image 925
Svish Avatar asked Jan 06 '10 11:01

Svish


2 Answers

You would need to solve the Halting Problem! That would get you rich and famous :)

like image 52
Mongus Pong Avatar answered Oct 08 '22 18:10

Mongus Pong


It's evil but you can spin it up in a new process. Launch the process from the unit test and wait for it to complete and check the result.

like image 22
Mike Two Avatar answered Oct 08 '22 18:10

Mike Two