TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# StackOverflowException

This C# exception article demonstrates StackOverflowException.

StackOverflowException. The stack has limited memory.

It can overflow. Typically the StackOverflowException is triggered by a recursive method that creates a deep call stack. The problem is linked to the concept of the stack memory region in general.

Example. This program defines a method that causes an infinite recursion at runtime. The Recursive method calls itself at the end of each invocation. An optimizing compiler could turn this method into a tail recursive call.

Recursion

However: The current C# compiler does not achieve this. Each method call frame (activation record) is kept on the stack memory.

Compiler

After nearly 80,000 invocations, the stack memory space is exhausted and the program terminates. Usually, the StackOverflowException is caused by an infinite or uncontrolled recursion.

C# program that generates StackOverflowException

using System;

class Program
{
    static void Recursive(int value)
    {
	// Write call number and call this method again.
	// ... The stack will eventually overflow.
	Console.WriteLine(value);
	Recursive(++value);
    }

    static void Main()
    {
	// Begin the infinite recursion.
	Recursive(0);
    }
}

Output

79845
79846
79847
79848
79849
79850
79851

Process is terminated due to StackOverflowException.

The final numbers printed by the program execution are displayed in the Output section. This shows that the runtime was able to call this trivial recursive method nearly 80,000 times before the stack memory region was out of space.

The message "Process is terminated" is displayed at this point and no recovery is possible. If you wrap the initial call to Recursive in a try-catch block, you cannot catch the StackOverflowException. The program is unsalvageable.

TryCatch

Tail recursion and constant space. The Recursive method body here contains a single call to the same method in its final statement. This could be rewritten as a linear loop, which would increment a counter variable and print its value.

And: Such a loop could continue indefinitely because it requires constant space on the stack.

But: The C# compiler was unable to apply this optimization, called tail recursion, in this program.

Summary. We tested a degenerate program that creates an infinite recursion to demonstrate the StackOverflowException. The program causes an unrecoverable error. The stack space varies based on the host computer, but is in all cases finite.

Warning: This exception is a real risk to all programs—theoretically even those that do not use recursion.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf