An unhandled exception of type systemstackoverflowexception occurred in mscorlibdll

An unhandled exception of type systemstackoverflowexception occurred in mscorlibdll


  You might be intrested in what caused to produce Stack Overflow error. Here I will explain it with infinite looping
of functino using Recursion. This happens when there is no memory to to allocate function on Stack.
The memory used for stack is very limited and it varies from program to program.
When program tries to access the memory beond the stack that is buffer overflow known as stack overflow.
There are two types of program which can cause the stack overflow

1. The Recursino which never breaks the condition of going out.

2. A variable which is very large to allocate on Stack.
 
Code: 

using System;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int counter = RecursionDemo(0);
        }
        public static int RecursionDemo(int i)
        {           
            i++;
            return RecursionDemo(i);
        }
    }
} 

Result


 system.stackoverflowexception

Another example at runtime.

Exception of type 'System.OutOfMemoryException' was thrown.

static void Main(string[] args)
        {
            int[] arr = new int[500000000];
        }

 

 

Tags:

An unhandled exception of type 'System.StackOverflowException' occurred inC# ArrayList Search using Linq Extension OfType MethodHtml5 Doctype Headersystem.outofmemoryexception was thrownSystemOutOfMemoryException was thrown (mscorlib)Linq Anonymous TypesMembership ProviderC# read excel xlsx 2007C# Recursion ExampleWhat is the purpose of virtual keyboard on bank websitesJquery Radio Button Checked

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.