Help me understand "StackOverflowException"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jesee

    Help me understand "StackOverflowException"

    I am reading Jeffrey Richter's book "Applied Microsoft .NET Framework
    programming",i came across "Exception handing". Page 405 says
    "If the stack overflow occurs within the CLR itself,your application code
    won't be able to catch the StackOverflowEx ception exception and none of your
    finally blocks will excute.",I don't understand it.
    Following C# statement:
    class App
    {
    static void Main()
    {
    try
    {
    Console.WriteLi ne("This process will throw a StackOverflowEx ception");
    ThrowStack();
    }
    catch (Exception ex)
    {
    Console.WriteLi ne(ex.Message);
    }
    finally
    {
    Console.WriteLi ne("Finally block execute");
    Console.ReadLin e();
    }
    }

    static void ThrowStack()
    {
    try
    {
    throw new StackOverflowEx ception("StackO verflowExceptio n is be throw by
    user");
    }
    catch (StackOverflowE xception ex)
    {
    throw ex;
    }
    }
    }
    output:

    This process will throw a StackOverflowEx ception
    StackOverflowEx ception is be throw by user
    Finally block execute

    It seems catch block catch this exception and finally black execute,why?

    Thanks for your help




  • Bruno Jouhier [MVP]

    #2
    Re: Help me understand "StackOver flowException&q uot;

    Jeffrey's comment applies to real stack overflows, i.e. to stack overflows
    that occur if your code contains an infinite recursion, something like:

    void MyMethod() { MyMethod(); }

    If you throw the StackOverflowEx ception yourself, it will be handled like
    any other exception, and Jeffrey's comment does not apply.

    Also, Jeffrey's comment says: "if the stack overflow occurs WITHIN THE CLR
    ITSELF ...". So, if the .NET VM can detect the stack overflow "cleanly",
    i.e. without running ITSELF into a stack overflow, then you should get a
    StackOverflowEx ception and your catch and finally blocks should execute as
    usual. But, in the tragic case where the VM ITSELF runs into a stack
    overflow, you won't be as lucky: the VM will not propagate a
    StackOverflowEx ception (but crash in some other weird way) and your catch
    and finally blocks won't execute.

    Morale is: be careful with infinite recursion because you don't have a 100%
    guarantee that the VM will detect and signal them cleanly!

    Bruno.

    "Jesee" <qingzc@163.net > a écrit dans le message de
    news:O%23IkAxmU DHA.2364@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > I am reading Jeffrey Richter's book "Applied Microsoft .NET Framework
    > programming",i came across "Exception handing". Page 405 says
    > "If the stack overflow occurs within the CLR itself,your application code
    > won't be able to catch the StackOverflowEx ception exception and none of[/color]
    your[color=blue]
    > finally blocks will excute.",I don't understand it.
    > Following C# statement:
    > class App
    > {
    > static void Main()
    > {
    > try
    > {
    > Console.WriteLi ne("This process will throw a StackOverflowEx ception");
    > ThrowStack();
    > }
    > catch (Exception ex)
    > {
    > Console.WriteLi ne(ex.Message);
    > }
    > finally
    > {
    > Console.WriteLi ne("Finally block execute");
    > Console.ReadLin e();
    > }
    > }
    >
    > static void ThrowStack()
    > {
    > try
    > {
    > throw new StackOverflowEx ception("StackO verflowExceptio n is be throw[/color]
    by[color=blue]
    > user");
    > }
    > catch (StackOverflowE xception ex)
    > {
    > throw ex;
    > }
    > }
    > }
    > output:
    >
    > This process will throw a StackOverflowEx ception
    > StackOverflowEx ception is be throw by user
    > Finally block execute
    >
    > It seems catch block catch this exception and finally black execute,why?
    >
    > Thanks for your help
    >
    >
    >
    >[/color]


    Comment

    • Gheorghe Marius

      #3
      Re: Help me understand &quot;StackOver flowException&q uot;

      Hello Bruno,

      Do you onestly think that is even a 1% chance for CLR to run into a Stack
      overflow ?

      Cheers,
      Marius.


      Comment

      • Jesee

        #4
        Re: Help me understand &quot;StackOver flowException&q uot;

        You said "the VM ITSELF runs into a stack
        overflow ,the CLR will not propagate a StackOverflowEx ception" £¬i.e. catch
        bolck statement won't catch "StackOverflowE xception" and finally block
        statement won't execute.
        but Following C# statement:
        namespace Test
        {
        class App
        {
        static void Main()
        {
        try
        {
        InfiniteLoop();
        }
        catch (StackOverflowE xception)
        {
        Console.WriteLi ne("StackOverfl owException occur");
        }
        finally
        {
        Console.WriteLi ne("Finally block execute");
        Console.ReadLin e();
        }
        }

        static void InfiniteLoop()
        {
        InfiniteLoop();
        }
        }
        }

        output:
        StackOverflowEx ception occur
        Finally block execute

        as you say,the catch block won't catch StackOverflowEx ception and finally
        block statement won't executed.but,re ally,the finally block statement
        executed.Why?


        Thanks for your help.

        }"Bruno Jouhier [MVP]" <bjouhier@clu b-internet.fr> дÈëÏûÏ¢
        news:OJvyyDnUDH A.656@tk2msftng p13.phx.gbl...[color=blue]
        > Jeffrey's comment applies to real stack overflows, i.e. to stack overflows
        > that occur if your code contains an infinite recursion, something like:
        >
        > void MyMethod() { MyMethod(); }
        >
        > If you throw the StackOverflowEx ception yourself, it will be handled like
        > any other exception, and Jeffrey's comment does not apply.
        >
        > Also, Jeffrey's comment says: "if the stack overflow occurs WITHIN THE CLR
        > ITSELF ...". So, if the .NET VM can detect the stack overflow "cleanly",
        > i.e. without running ITSELF into a stack overflow, then you should get a
        > StackOverflowEx ception and your catch and finally blocks should execute as
        > usual. But, in the tragic case where the VM ITSELF runs into a stack
        > overflow, you won't be as lucky: the VM will not propagate a
        > StackOverflowEx ception (but crash in some other weird way) and your catch
        > and finally blocks won't execute.
        >
        > Morale is: be careful with infinite recursion because you don't have a[/color]
        100%[color=blue]
        > guarantee that the VM will detect and signal them cleanly!
        >
        > Bruno.
        >
        > "Jesee" <qingzc@163.net > a écrit dans le message de
        > news:O%23IkAxmU DHA.2364@TK2MSF TNGP09.phx.gbl. ..[color=green]
        > > I am reading Jeffrey Richter's book "Applied Microsoft .NET Framework
        > > programming",i came across "Exception handing". Page 405 says
        > > "If the stack overflow occurs within the CLR itself,your application[/color][/color]
        code[color=blue][color=green]
        > > won't be able to catch the StackOverflowEx ception exception and none of[/color]
        > your[color=green]
        > > finally blocks will excute.",I don't understand it.
        > > Following C# statement:
        > > class App
        > > {
        > > static void Main()
        > > {
        > > try
        > > {
        > > Console.WriteLi ne("This process will throw a[/color][/color]
        StackOverflowEx ception");[color=blue][color=green]
        > > ThrowStack();
        > > }
        > > catch (Exception ex)
        > > {
        > > Console.WriteLi ne(ex.Message);
        > > }
        > > finally
        > > {
        > > Console.WriteLi ne("Finally block execute");
        > > Console.ReadLin e();
        > > }
        > > }
        > >
        > > static void ThrowStack()
        > > {
        > > try
        > > {
        > > throw new StackOverflowEx ception("StackO verflowExceptio n is be throw[/color]
        > by[color=green]
        > > user");
        > > }
        > > catch (StackOverflowE xception ex)
        > > {
        > > throw ex;
        > > }
        > > }
        > > }
        > > output:
        > >
        > > This process will throw a StackOverflowEx ception
        > > StackOverflowEx ception is be throw by user
        > > Finally block execute
        > >
        > > It seems catch block catch this exception and finally black execute,why?
        > >
        > > Thanks for your help
        > >
        > >
        > >
        > >[/color]
        >
        >[/color]



        Comment

        • Bruno Jouhier [MVP]

          #5
          Re: Help me understand &quot;StackOver flowException&q uot;

          Definitely! I ran into it today, by coincidence. The VM crashed with a
          strange message about the debugger and the JIT compiler getting into each
          other's way, because I had an infinite recursion (had not happened to me for
          a long while but it had to be today ;-)).
          So, yes, "shit happens".

          Bruno.

          "Gheorghe Marius" <cghostsoft@hot mail.com> a écrit dans le message de
          news:Ov43GonUDH A.2104@TK2MSFTN GP10.phx.gbl...[color=blue]
          > Hello Bruno,
          >
          > Do you onestly think that is even a 1% chance for CLR to run into a Stack
          > overflow ?
          >
          > Cheers,
          > Marius.
          >
          >[/color]


          Comment

          • Bruno Jouhier [MVP]

            #6
            Re: Help me understand &quot;StackOver flowException&q uot;

            Why?

            Because you were lucky! The VM noticed that the stack was going to overflow
            early enough, and there was enough stack left so that VM could allocate the
            StackOverflowEx ception and throw it. And everything worked as expected.

            But there are situations were you might not be as lucky (I ran into one
            today, by pure coincidence but I can't reproduce it on a simple case) and
            were the VM does not have enough stack left to handle the overflow cleanly.
            At some point the VM needs to call new StackOverflowEx ception to create the
            exception, and this causes a call to the memory manager, which calls some
            internal methods, so you need a bit a stack to do this. In these ugly cases,
            the VM does not have enough stack and crashes with a weird message!

            Bruno.

            "Jesee" <qingzc@163.net > a écrit dans le message de
            news:%23nU5HxnU DHA.2284@TK2MSF TNGP11.phx.gbl. ..[color=blue]
            > You said "the VM ITSELF runs into a stack
            > overflow ,the CLR will not propagate a StackOverflowEx ception" £¬i.e.[/color]
            catch[color=blue]
            > bolck statement won't catch "StackOverflowE xception" and finally block
            > statement won't execute.
            > but Following C# statement:
            > namespace Test
            > {
            > class App
            > {
            > static void Main()
            > {
            > try
            > {
            > InfiniteLoop();
            > }
            > catch (StackOverflowE xception)
            > {
            > Console.WriteLi ne("StackOverfl owException occur");
            > }
            > finally
            > {
            > Console.WriteLi ne("Finally block execute");
            > Console.ReadLin e();
            > }
            > }
            >
            > static void InfiniteLoop()
            > {
            > InfiniteLoop();
            > }
            > }
            > }
            >
            > output:
            > StackOverflowEx ception occur
            > Finally block execute
            >
            > as you say,the catch block won't catch StackOverflowEx ception and finally
            > block statement won't executed.but,re ally,the finally block statement
            > executed.Why?
            >
            >
            > Thanks for your help.
            >
            > }"Bruno Jouhier [MVP]" <bjouhier@clu b-internet.fr> дÈëÏûÏ¢
            > news:OJvyyDnUDH A.656@tk2msftng p13.phx.gbl...[color=green]
            > > Jeffrey's comment applies to real stack overflows, i.e. to stack[/color][/color]
            overflows[color=blue][color=green]
            > > that occur if your code contains an infinite recursion, something like:
            > >
            > > void MyMethod() { MyMethod(); }
            > >
            > > If you throw the StackOverflowEx ception yourself, it will be handled[/color][/color]
            like[color=blue][color=green]
            > > any other exception, and Jeffrey's comment does not apply.
            > >
            > > Also, Jeffrey's comment says: "if the stack overflow occurs WITHIN THE[/color][/color]
            CLR[color=blue][color=green]
            > > ITSELF ...". So, if the .NET VM can detect the stack overflow "cleanly",
            > > i.e. without running ITSELF into a stack overflow, then you should get a
            > > StackOverflowEx ception and your catch and finally blocks should execute[/color][/color]
            as[color=blue][color=green]
            > > usual. But, in the tragic case where the VM ITSELF runs into a stack
            > > overflow, you won't be as lucky: the VM will not propagate a
            > > StackOverflowEx ception (but crash in some other weird way) and your[/color][/color]
            catch[color=blue][color=green]
            > > and finally blocks won't execute.
            > >
            > > Morale is: be careful with infinite recursion because you don't have a[/color]
            > 100%[color=green]
            > > guarantee that the VM will detect and signal them cleanly!
            > >
            > > Bruno.
            > >
            > > "Jesee" <qingzc@163.net > a écrit dans le message de
            > > news:O%23IkAxmU DHA.2364@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
            > > > I am reading Jeffrey Richter's book "Applied Microsoft .NET Framework
            > > > programming",i came across "Exception handing". Page 405 says
            > > > "If the stack overflow occurs within the CLR itself,your application[/color][/color]
            > code[color=green][color=darkred]
            > > > won't be able to catch the StackOverflowEx ception exception and none[/color][/color][/color]
            of[color=blue][color=green]
            > > your[color=darkred]
            > > > finally blocks will excute.",I don't understand it.
            > > > Following C# statement:
            > > > class App
            > > > {
            > > > static void Main()
            > > > {
            > > > try
            > > > {
            > > > Console.WriteLi ne("This process will throw a[/color][/color]
            > StackOverflowEx ception");[color=green][color=darkred]
            > > > ThrowStack();
            > > > }
            > > > catch (Exception ex)
            > > > {
            > > > Console.WriteLi ne(ex.Message);
            > > > }
            > > > finally
            > > > {
            > > > Console.WriteLi ne("Finally block execute");
            > > > Console.ReadLin e();
            > > > }
            > > > }
            > > >
            > > > static void ThrowStack()
            > > > {
            > > > try
            > > > {
            > > > throw new StackOverflowEx ception("StackO verflowExceptio n is be[/color][/color][/color]
            throw[color=blue][color=green]
            > > by[color=darkred]
            > > > user");
            > > > }
            > > > catch (StackOverflowE xception ex)
            > > > {
            > > > throw ex;
            > > > }
            > > > }
            > > > }
            > > > output:
            > > >
            > > > This process will throw a StackOverflowEx ception
            > > > StackOverflowEx ception is be throw by user
            > > > Finally block execute
            > > >
            > > > It seems catch block catch this exception and finally black[/color][/color][/color]
            execute,why?[color=blue][color=green][color=darkred]
            > > >
            > > > Thanks for your help
            > > >
            > > >
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >
            >[/color]


            Comment

            Working...