According to
at the moment it's not possible to catch StackOverflowException programmatically, but I was wondering if it's possible to use debugger api (Debug or Debugger) and react to stack overflow exception?
Has anyone done something similar?
According to
at the moment it's not possible to catch StackOverflowException programmatically, but I was wondering if it's possible to use debugger api (Debug or Debugger) and react to stack overflow exception?
Has anyone done something similar?
A Stack Overflow caused by an endless recursive method call or an infinite loop actually saturates the stack of the current processus which leads to an unrecoverable process crash like a bulldozer that has crushed the entire alley of trees.
It is then impossible for the current process to catch this exception which goes out of the context of the process that lost the control and "does not exist anymore", and then only a debugger or a host application as explained in the link you provided can retrieve this information.
Nothing can be done at this point.
So before Windows 95 and especially NT, a stack overflow in a process usually and relentlessly caused a system crash because the processes were not isolated, not being in protected mode, and they crushed the whole land.
https://www.tutorialspoint.com/operating_system/os_processes.htm
Protected virtual address mode
So you can't use a debugger API.
But you can write a special host that runs your app and controls it like the debugger do, perhaps for example:
Hosting EXE Applications in a WinForm project (CodeProject)
Creating a host application for the .NET Common Language Runtime (CodeProject)
Using HostBuilder and the Generic Host in .NET Core Microservices
You can monitor your current call stack framecount using System.Diagnostics.Stacktrace.FrameCount. No way to determine the max framecount however, but if this number gets up very high you can bet you'll get in trouble soon.