The tutorial for today is about StackOverflowException.
The exception is thrown when the execution stack overflows by having too many pending method calls. This class cannot be inherited.
Microsoft intermediate language (MSIL) instruction allocates a certain number of bytes from the local dynamic memory pool and pushes the address (a transient pointer, type *) of the first allocated byte onto the evaluation stack.
The stack transitional behavior, in sequential order, is:
- the number of bytes to be allocated is pushed onto the stack.
- the number of bytes is popped from the stack; an amount of memory corresponding to the size is allocated from the local heap.
- a pointer to the first byte of the allocated memory is pushed onto the stack.
StackOverflowException uses the HRESULT COR_E_STACKOVERFLOW, which has the value 0x800703E9, see the official webpage.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | using System; using System.IO; namespace StackOverflowSpace { /// <summary> /// This class contains the entry point for a very simple program /// that will illustrate the StackOverflowException. The exception /// will be thrown from inside the function call but will keep /// traversing up the stack until we catch it inside Main. /// </summary> class StackOverflowExample { //entry point for the sample static void Main() { //attempt to call an infinitely recursive function try { Recurse.InfiniteRecurse(); } //this will fire when the recursion went too deep catch (System.StackOverflowException overFlowExcept) { Console.WriteLine(overFlowExcept.Message); Console.WriteLine("Program will now end."); return; } Console.WriteLine("This line will never execute"); } //end of main } //end of class Stackoverflow /// <summary> /// Recurse contains one static member designed to recurse forever. /// We could try to catch it inside this function and deal with it /// here, but it's hard to do much without any stack left. /// </summary> class Recurse { //keeps calling itself forever until it //overflows the stack and throws a exception static public void InfiniteRecurse() { InfiniteRecurse(); return; } } //end of class Recurse } //end of StackOverFlowSpace Namespace |