1

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?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • The answer is: *because the spec says so*, the reason is that when a stack overflow happens, the process state is too unreliable for the system to even be sure that you'd actually make it to that `catch` part – Jcl Jan 26 '15 at 09:44

1 Answers1

3

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.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thanks.This is sample project.I have an application that has app pool and the apppoll frequently restart.i write this code for testing. –  Jan 26 '15 at 09:47
  • I understood that it was test code already. The answer is still: prevent it, just like almost every exception. – Patrick Hofman Jan 26 '15 at 09:48
  • C# doesn't eliminate tail calls, so you probably mean "rewrite the algorithm as a loop"? – Clément May 13 '22 at 23:42