C#: How to test for StackOverflowException
c#, nunit, stack-overflow, unit-testing
Solution
You would need to solve the Halting Problem! That would get you rich and famous :)
Problem
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?