Q:

C# program to demonstrate the stack overflow exception

belongs to collection: C# Exception Handling Programs

0

C# program to demonstrate the stack overflow exception

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to demonstrate the stack overflow exception is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate the multiple catch blocks

using System;

class ExceptionDemo
{
    static void Main(string[] args)
    {
        int num1=0;
        int num2=0;
        int num3=0;
        
        try
        {
            Console.Write("Enter the value of num1: "); 
            num1 = int.Parse(Console.ReadLine());

            Console.Write("Enter the value of num2: ");
            num2 = int.Parse(Console.ReadLine());

            num3 = num1 / num2;

            Console.WriteLine("Num3 : " + num3);
        }
        catch (DivideByZeroException exp)
        {
            Console.WriteLine(exp.Message);
        }
        catch (FormatException)
        {
            Console.WriteLine("Exception: Invalid format");
        }
        catch (Exception)
        {
            Console.WriteLine("Other exception generated");
        }
    }
}

Output:

Process is terminated due to StackOverflowException.
Press any key to continue . . .

Explanation:

In the above program, we created a class ExceptionDemo that contains two methods increaseVal() and Main(). The increaseVal() is a recursive method, here we increase the value of the parameter, due to recursion, method calls indefinitely then StackOverflowException exception get generated by the program that will be caught by "catch" block and print exception message on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# program to demonstrate the NullReferenceExcepti... >>
<< C# program to demonstrate the multiple catch block...