I recently saw a funny piece of code of a function returning the number five.
This is what it looks like:
namespace Rekursio {
class Program {
static void Main(string[] args) {
Console.WriteLine(getFive());
Console.ReadLine();
}
static int getFive() {
try {
return getFive();
}
catch (StackOverflowException e) {
return 5;
}
}
}
}
I expected this function to return 5 at the end. When running the program, however, I am told that an unhandled StackOverflowException has occurred.
How is this possible? Every situation in which a StackOverflowException is possible is handled.