class Program
{
public static void method()
{
int[] array = new int[100];
Console.WriteLine(array);
method(); //recursion
}
static void Main()
{
try
{
method();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
//finally does not work
Console.WriteLine("i am finally");
}
}
}
StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion.
StackOverflowException uses the HRESULT COR_E_STACKOVERFLOW, which has the value 0x800703E9. The Localloc intermediate language (IL) instruction throws StackOverflowException. For a list of initial property values for a StackOverflowException object, see the StackOverflowException constructors.
In the .NET Framework 1.0 and 1.1, you could catch a StackOverflowException object (for example, to recover from unbounded recursion). Starting with the .NET Framework 2.0, you can’t catch a StackOverflowException object with a try/catch block, and the corresponding process is terminated by default. Consequently, you should write your code to detect and prevent a stack overflow. For example, if your app depends on recursion, use a counter or a state condition to terminate the recursive loop. The following example uses a counter to ensure that the number of recursive calls to the Execute method do not exceed a maximum defined by the MAX_RECURSIVE_CALLS constant.