Without using recursion, how can an exception be thrown?

Without recursion, how to throw an exception?

+5
stack exception stack-overflow recursion
source share
9 answers

If you call enough methods, stack overflows can occur at any time. Although, if you get errors without using recursion, you can rethink how you do it. It is so simple with recursion, because in an infinite loop you call a ton of methods.

+12
source share

Since no one mentioned this:

throw new System.StackOverflowException(); 

You can do this when testing or injecting.

+20
source share

Declare the ENORMOUS array as a local variable.

+16
source share

On Windows, the following applies, but most OSs implement this in a similar way.

Short answer: if you touch the last page of protection, it will throw.

An exception of type EXCEPTION_STACK_OVERFLOW (C00000FD) occurs when your application touches the bottom page of the stack that is marked with the PAGE_GUARD security icon and there is no room for the stack to grow (commit another page), see How to intercept stack overflow in a Visual C ++ application .
A typical case when this happens is when the stack has grown as a result of many functional frames in the stack (i.e., Out of control recursion), resulting in fewer frames, but very large frame sizes (functions with a very large local area of ​​the object) or path explicit allocation from the _alloca stack.
Another way to throw an exception is simply to deliberately touch the protection page, for example. by dereferencing a pointer that points to this page. This may be due to an initialization variable error.

Stack overflows can occur on valid execution paths if input causes a very deep level of nesting. For example, see Stack overflow occurs when you run a query that contains a large number of arguments inside an IN or NOT IN clause in SQL Server.

+7
source share

Each method call that has not yet returned consumes some stack space. (Methods with more local variables consume more space.) A very deep call stack can lead to a stack overflow.

Please note that on systems with limited memory (mobile devices, etc.) you do not have much space on the stack and will exit sooner.

+2
source share

The short answer is: if you have an object that calls an internal object, you increase the stack trace by 1. Thus, if you have 1000 objects nested inside each other, each of which calls its own internal object, you end up with overflow stack.

Here's how to create primes using nested iterators:

 using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Program p = new Program(); IEnumerator<int> primes = p.AllPrimes().GetEnumerator(); int numberOfPrimes = 1000; for (int i = 0; i <= numberOfPrimes; i++) { primes.MoveNext(); if (i % 1000 == 0) { Console.WriteLine(primes.Current); } } Console.ReadKey(true); } IEnumerable<int> FilterDivisors(IEnumerator<int> seq, int num) { while (true) { int current = seq.Current; if (current % num != 0) { yield return current; } seq.MoveNext(); } } IEnumerable<int> AllIntegers() { int i = 2; while (true) { yield return i++; } } IEnumerable<int> AllPrimes() { IEnumerator<int> nums = AllIntegers().GetEnumerator(); while (true) { nums.MoveNext(); int prime = nums.Current; yield return prime; // nested iterator makes a big boom nums = FilterDivisors(nums, prime).GetEnumerator(); } } } } 

There is no recursion, but the program will throw an exception after about 150,000 primes.

+2
source share

If you are talking about C ++ with a reasonable standard library, I think this will work:

 while (true) { alloca(1024 * 1024); // arbitrary - 1M per iteration. } 

Details of alloca .

+1
source share
 int main() { //something on the stack int foo = 0; for ( //pointer to an address on the stack int* p = &foo; //forever ; //ever lower on the stack (assuming that the stack grows downwards) --p) { //write to the stack *p = 42; } } 
0
source share

The easiest way to make a StackOverflowException is as follows:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { SomeClass instance = new SomeClass(); string name = instance.Name; } } public class SomeClass { public string Name { get { return Name; } } } } 
-one
source share

All Articles