简体   繁体   中英

in C# can I have code execute when an exception is thrown?

I've got a bug that is really difficult for me to track. It's one of those that only occurs when the debugger isnt connected :)

I've pinvoked MiniDumpWriteDump() and that works really well for creating .dmp files that I can debug after the fact.

however, I'm doing something like

try
{
    foo();
}
catch(Exception)
{
    CreateMiniDump()
}

this works great, however I dont get the callstack that is desired. I'd really prefer to have the callstack (with all the member variables) at the point where the exception was thrown.

is this possible? Is there a mechanism to get first dibs on the exception? such that I can create a minidump and preserve the callstack, etc?

You can subscribe to AppDomain.CurrentDomain.UnhandledException to get an event that is raised when an exception is about to unwind. When it's raised, the original context is still on the stack. You could put your minidump creation code inside of your event handler.

在.NET 4及更高版本中,您可以订阅FirstChanceException事件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM