473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 StackOverflowException exception and none of your
finally blocks will excute.",I don't understand it.
Following C# statement:
class App
{
static void Main()
{
try
{
Console.WriteLine("This process will throw a StackOverflowException");
ThrowStack();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Finally block execute");
Console.ReadLine();
}
}

static void ThrowStack()
{
try
{
throw new StackOverflowException("StackOverflowException is be throw by
user");
}
catch (StackOverflowException ex)
{
throw ex;
}
}
}
output:

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

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

Thanks for your help


Nov 15 '05 #1
5 17233
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 StackOverflowException 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
StackOverflowException 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
StackOverflowException (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" <qi****@163.net> a écrit dans le message de
news:O%****************@TK2MSFTNGP09.phx.gbl...
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 StackOverflowException exception and none of your finally blocks will excute.",I don't understand it.
Following C# statement:
class App
{
static void Main()
{
try
{
Console.WriteLine("This process will throw a StackOverflowException");
ThrowStack();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Finally block execute");
Console.ReadLine();
}
}

static void ThrowStack()
{
try
{
throw new StackOverflowException("StackOverflowException is be throw by user");
}
catch (StackOverflowException ex)
{
throw ex;
}
}
}
output:

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

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

Thanks for your help

Nov 15 '05 #2
Hello Bruno,

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

Cheers,
Marius.
Nov 15 '05 #3
You said "the VM ITSELF runs into a stack
overflow ,the CLR will not propagate a StackOverflowException" £¬i.e. catch
bolck statement won't catch "StackOverflowException" and finally block
statement won't execute.
but Following C# statement:
namespace Test
{
class App
{
static void Main()
{
try
{
InfiniteLoop();
}
catch (StackOverflowException)
{
Console.WriteLine("StackOverflowException occur");
}
finally
{
Console.WriteLine("Finally block execute");
Console.ReadLine();
}
}

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

output:
StackOverflowException occur
Finally block execute

as you say,the catch block won't catch StackOverflowException and finally
block statement won't executed.but,really,the finally block statement
executed.Why?
Thanks for your help.

}"Bruno Jouhier [MVP]" <bj******@club-internet.fr> дÈëÏûÏ¢
news:OJ*************@tk2msftngp13.phx.gbl...
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 StackOverflowException 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
StackOverflowException 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
StackOverflowException (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" <qi****@163.net> a écrit dans le message de
news:O%****************@TK2MSFTNGP09.phx.gbl...
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 StackOverflowException exception and none of

your
finally blocks will excute.",I don't understand it.
Following C# statement:
class App
{
static void Main()
{
try
{
Console.WriteLine("This process will throw a StackOverflowException"); ThrowStack();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Finally block execute");
Console.ReadLine();
}
}

static void ThrowStack()
{
try
{
throw new StackOverflowException("StackOverflowException is be throw

by
user");
}
catch (StackOverflowException ex)
{
throw ex;
}
}
}
output:

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

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

Thanks for your help



Nov 15 '05 #4
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" <cg********@hotmail.com> a écrit dans le message de
news:Ov**************@TK2MSFTNGP10.phx.gbl...
Hello Bruno,

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

Cheers,
Marius.

Nov 15 '05 #5
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
StackOverflowException 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 StackOverflowException 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" <qi****@163.net> a écrit dans le message de
news:%2****************@TK2MSFTNGP11.phx.gbl...
You said "the VM ITSELF runs into a stack
overflow ,the CLR will not propagate a StackOverflowException" £¬i.e. catch bolck statement won't catch "StackOverflowException" and finally block
statement won't execute.
but Following C# statement:
namespace Test
{
class App
{
static void Main()
{
try
{
InfiniteLoop();
}
catch (StackOverflowException)
{
Console.WriteLine("StackOverflowException occur");
}
finally
{
Console.WriteLine("Finally block execute");
Console.ReadLine();
}
}

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

output:
StackOverflowException occur
Finally block execute

as you say,the catch block won't catch StackOverflowException and finally
block statement won't executed.but,really,the finally block statement
executed.Why?
Thanks for your help.

}"Bruno Jouhier [MVP]" <bj******@club-internet.fr> дÈëÏûÏ¢
news:OJ*************@tk2msftngp13.phx.gbl...
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 StackOverflowException 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
StackOverflowException 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
StackOverflowException (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" <qi****@163.net> a écrit dans le message de
news:O%****************@TK2MSFTNGP09.phx.gbl...
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 StackOverflowException exception and none of your
finally blocks will excute.",I don't understand it.
Following C# statement:
class App
{
static void Main()
{
try
{
Console.WriteLine("This process will throw a StackOverflowException"); ThrowStack();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Finally block execute");
Console.ReadLine();
}
}

static void ThrowStack()
{
try
{
throw new StackOverflowException("StackOverflowException is be
throw by
user");
}
catch (StackOverflowException ex)
{
throw ex;
}
}
}
output:

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

It seems catch block catch this exception and finally black

execute,why?
Thanks for your help



Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: cnu | last post by:
Hi I have to write images(.gif/.bmp/.jpg/.ico), to db and read them. Uploading images to db works fine for me. Reading from db to byte is also ok. But, when I try to display them in my form...
1
by: bunnyjoshi | last post by:
"An unhandled exception of type 'System.StackOverflowException' occurred in asset.dll" Above error is occuring when i m debugging my code. asset.dll is file which is created by me n i m using it...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.