Search code examples
c#.netwcfstack-overflow

How to handle a StackOverflowException


Consider this code:

[GlobalErrorBehaviorAttribute(typeof(GlobalErrorHandler))]
public class Service1 : IService1
{
    public string Recursive(int value)
    {
        Recursive(value);
        return string.Format("You entered: {0}", value);
    }

and this is my GlobalErrorHandler:

public class GlobalErrorHandler : IErrorHandler
{
    public bool HandleError(Exception error)
    {
        string path = HostingEnvironment.ApplicationPhysicalPath;

        using (TextWriter tw = File.AppendText(Path.Combine(path, @"d:\\IIS.Log")))
        {
            if (error != null)
            {
                tw.WriteLine("Exception:{0}{1}Method: {2}{3}Message:{4}",
                    error.GetType().Name, Environment.NewLine, error.TargetSite.Name,
                    Environment.NewLine, error.Message + Environment.NewLine);
            }
            tw.Close();
        }

        return true;
    }

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
        var newEx = new FaultException(
                     string.Format("Exception caught at GlobalErrorHandler{0}Method: {1}{2}Message:{3}",
                                  Environment.NewLine, error.TargetSite.Name, Environment.NewLine, error.Message));

        MessageFault msgFault = newEx.CreateMessageFault();
        fault = Message.CreateMessage(version, msgFault, newEx.Action);
    }
}

When I call Recursive in the WCF Test Client I get this error. Why can't I handle a StackOverflowException?

Is there any way for handle such this error?


Solution

  • According to MSDN:

    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.

    In this case, it means you should actively prevent the exception by passing in a integer checking if the depth gets to low and throw an exception yourself, something like this:

    public string Recursive(int value, int counter)
    {
        if (counter > MAX_RECURSION_LEVEL) throw new Exception("Too bad!");
    
        Recursive(value, counter + 1);
        return string.Format("You entered: {0}", value);
    }
    

    Or rewrite the algorithm to use tail recursion.