Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Without using recursion how can a stack overflow exception be thrown?

Without using recursion how can a stack overflow exception be thrown?

like image 912
JaredCacurak Avatar asked Oct 17 '09 23:10

JaredCacurak


People also ask

Can stack overflow only happen with recursion?

However, some compilers implement tail-call optimization, allowing infinite recursion of a specific sort—tail recursion—to occur without stack overflow. This works because tail-recursion calls do not take up additional stack space.

How does stack overflow prevent recursion?

In order to prevent stack overflow bugs, you must have a base case where the function stops make new recursive calls. If there is no base case then the function calls will never stop and eventually a stack overflow will occur.

How do I get stack overflow exception?

StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion. So make sure your code doesn't have an infinite loop or infinite recursion. StackOverflowException uses the HRESULT COR_E_STACKOVERFLOW, which has the value 0x800703E9.


1 Answers

Since no one else has mentioned it:

throw new System.StackOverflowException();

You might do this when testing or doing fault-injection.

like image 76
Brian Avatar answered Sep 27 '22 19:09

Brian