AppDomain.FirstChanceException and stack overflow exception

前端 未结 7 1693

I\'m using the FirstChanceException event to log details about any thrown exceptions.

static void Main(string[] args)
{
    AppDomain.CurrentDomain.FirstChanceEx         


        
7条回答
  •  借酒劲吻你
    2021-02-05 11:17

    The MSDN article you linked makes a few recommandations:

    You must handle all exceptions that occur in the event handler for the FirstChanceException event. Otherwise, FirstChanceException is raised recursively. This could result in a stack overflow and termination of the application. We recommend that you implement event handlers for this event as constrained execution regions (CERs), to keep infrastructure-related exceptions such as out-of-memory or stack overflow from affecting the virtual machine while the exception notification is being processed.

    So enclose your function inside a try/catch block, and call PrepareConstrainedRegion before the block to avoid OutOfMemory exceptions: http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.runtimehelpers.prepareconstrainedregions.aspx

    Edit: Well, you still have the recursion issue even with the try/catch block. So... I guess you just have to call only safe code that won't throw any exception. That event handler seems quite dangerous, I'd recommend to use it only for debugging purpose.

提交回复
热议问题