Introduction to C# StackOverflowException
The following article provides an outline for C# StackOverflowException. The exception that is raised whenever we are calling too many methods nested inside one another and every call creates a stack in memory on top one another resulting in an uncontrolled recursion which is infinite and terminates the program with no error message displayed using which recovery is not possible nor the exception can be caught by the try and catch blocks, is called StackOverflowException.
This StackOverflowException is thrown by the Microsoft Intermediate Language (MSIL) instruction called OpCodes.LocalLoc instruction and there are several methods of StackOverflowException class like StackOverflowException(), StackOverflowException(string message), StackOverflowException(string message, exception innerexception) etc.
Syntax :
[Serializable]
public sealed class StackOverflowException : SystemException
Working
- Whenever there is a need to call too many methods nested inside one another and every call creates a stack in memory on top one another resulting in an uncontrolled recursion which is infinite and terminates the program with no error message displayed using which recovery is not possible nor the exception can be caught by the try and catch blocks, is called StackOverflowException.
- StackOverflowException is thrown by the Microsoft Intermediate Language (MSIL) instruction called OpCodes.LocalLoc instruction.
- There are several methods of StackOverflowException class like StackOverflowException(), StackOverflowException(string message), StackOverflowException(string message, exception innerexception) etc.
Examples of C# StackOverflowException
Given below are the examples mentioned :
Example #1
C# program to demonstrate Stack Overflow Exception when there is an infinite recursion happening at the run time.
Code:
using System;
//a class called program is defined
public class program
{
// a method called rec is defined which takes a value as parameter and increases its value by one
static void Rec(int vals)
{
// since we have written a recursive loop and 0 is passed as a parameter, it ends in an infinite loop causing exception
Console.WriteLine(vals);
Rec(++vals);
}
//main method is called
public static void Main()
{
//The rec method is called to start the infinite recursion
Rec(0);
}
}
Output:
In the above program, a class called program is defined. Then a method called rec is defined which takes a value as parameter and increases its value by one. Then the main method is called in which the infinite loop for recursion begins by passing zero as parameter. Then since we have written a recursive loop and 0 is passed as a parameter, it ends in an infinite loop causing exception.
Example #2
C# program to demonstrate StackOverflowException when there is an infinite recursion happening at the run time even after using try block and catch blocks of code to catch the exception.
Code:
using System;
//a class called check is defined
public class check
{
// a method called ex is defined which takes a value as parameter and increases its value by one
static void ex(int equals)
{
Console.WriteLine(equals);
ex(++equals);
}
//main method is called within which try and block methods are defined to catch the exception
public static void Main()
{
try
{
//The ex method is called by passing zero as a parameter to start the infinite recursion
ex(0);
}
catch (StackOverflowException ep)
{
Console.WriteLine(ep.Message);
}
}
}
Output:
In the above program, a class called check is defined. Then a method called ex is defined which takes a value as parameter and increases its value by one. Then the main method is called in which the try block and catch blocks are defined. An infinite loop for recursion begins by passing zero as parameter to the ex method within try block. Then since we have written a recursive loop and 0 is passed as a parameter, it ends in an infinite loop causing exception. Even though we have written the catch block to catch the exception, it fails to catch this exception because this exception goes beyond the catch block to be caught.
Steps to Avoid StackOverflowException in C#
- Stack over flow happens when the stack in the memory is full and this problem can be overcome by setting the depth of the stack and this information can be provided by the debugger. Setting the size of the stack or the maximum depth value of the recursion is allowed in most of the programming languages.
- Now that we have an opportunity to set the value of the depth of the stack, we have to set to a value as small as possible and observe the output. If the output is not over flowing, we can change it to a bigger value and in case if the stack over flow happens, we will be able to decode what is the right value to be set for the depth of the stack.
- Since the StackOverflowException cannot be caught even though we can make use of try blocks of code and catch blocks of code, by knowing the depth of the stack which can be obtained by using the debugger, we can create our own exceptions. Hence to create the exceptions that can catch the StackOverflowException, knowing the depth of the stack by the help of debugger is a must.
- The code which causes recursion is the cause of StackOverflowException. Hence the code must be analyzed both manually and statistically and find out if there is anything that is going to cause infinite recursion in the program beforehand.
Recommended Articles
This is a guide to C# StackOverflowException. Here we discuss the introduction, working, examples and steps to avoid StackOverflowException. You may also have a look at the following articles to learn more –