If I write this:
class Program
{
static void Main(string[] args)
{
throw new Exception("lol");
}
}
and run the exe from the command line, I get two entries in my event log. One is an application error that says there was an unhandled exception, and the other contains the stack trace with the source as .NET Runtime.
If I write this:
class Program
{
static void Main(string[] args)
{
Recurse4Evr();
}
static void Recurse4Evr()
{
Recurse4Evr();
}
}
I only get one entry in my event log, which says Application Error and that there was a stack overflow exception. There isn't that second entry with the stack trace, so it's basically useless.
Why isn't the stack trace also logged? If I setup DebugDiag and attach it to my process and then there is a stack overflow, DebugDiag is able to log the stack trace. Obviously the stack trace is available to the outside world in some way. If the runtime is terminating the process because it detected a stackoverflow, then it also knows what the stack is.
In large applications that have many complex interactions, it is often not possible to recreate the conditions that led to a stack overflow. In this situation, a stack trace is the only way to figure out what happened. Why did microsoft decide it wasn't important to log this information? Was there a legitimate design decision that isn't obvious?
Stack overflow is considered a non-recoverable situation and for such the runtime kills the process.
to sum up:
from Damien_The_Unbeliever
From the MSDN page on StackOverflowExceptions:
In prior versions of the .NET Framework, your application could catch a StackOverflowException object (for example, to recover from unbounded recursion). However, that practice is currently discouraged because significant additional code is required to reliably catch a stack overflow exception and continue program execution.
Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow. For example, if your application depends on recursion, use a counter or a state condition to terminate the recursive loop. Note that an application that hosts the common language runtime (CLR) can specify that the CLR unload the application domain where the stack overflow exception occurs and let the corresponding process continue. For more information, see ICLRPolicyManager Interface and Hosting the Common Language Runtime.
from JaredPar
Starting with 2.0 a StackOverflow Exception can only be caught in the following circumstances.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With