在操作系统级别由于未捕获到异常,会发生什么,当一个.NET程序退出?异常、级别、操作系统、发生

2023-09-03 06:17:42 作者:十里故清欢

实际问题(S):

在Windows中当一个程序从一个未捕获的异常崩溃?

What happens "in Windows" when a program crashes from an uncaught exception?

有一个DLL函数,这是我能勾,以记录有关崩溃的一些基本信息?

Is there a dll function, which I can hook, to log some basic information about a crash?

背景:

我打算写一个程序,将收集哪些崩溃在我的本地PC上的任何应用程序的一些基本信息。我希望我可以执行一个简单的方法来记录有关崩溃的一些信息,以类似的方式到Visual Studio生成一个对话框要求让你调试程序时崩溃的方式。

I am planning to write a program which will collect some very basic information about any applications which crash on my local pc. I was hoping that I could execute a simple method to log some information about a crash in a similar manner to the way Visual Studio produces a dialog offering to let you debug a program when it crashes.

推荐答案

托管异常使用的是常规的Windows结构化异常处理机制来实现。唯一的例外code为0xe0434f4d。窗户去寻找一个异常处理程序,是愿意为处理异常。如果code的堆栈上没有活动的try块,或者没有catch块愿意捕捉托管异常,然后在管理code在奄奄一息的AppDomain.UnhandledException事件。

Managed exceptions are implemented using the regular Windows Structured Exception Handling mechanisms. The exception code is 0xe0434f4d. Windows goes looking for an exception handler that's willing to handle the exception. If the code has no active try block on the stack, or no catch block is willing to catch the managed exception then the last gasp in managed code is the AppDomain.UnhandledException event.

如果未实现比任何异常处理切换到非托管的处理,设置SetUnhandledExceptionFilter异常过滤器获得了一枪。如果做不到这一点,总是有Windows提供一个默认的处理程序。在正常调用WER,Windows错误报告程序。这为用户提供了一个对话框,以异常的详细信息发送给微软。不要指望什么了这一点。

If that isn't implemented either than exception handling switches to unmanaged handling, an exception filter set with SetUnhandledExceptionFilter gets a shot. Failing that, there is always a default handler provided by Windows. In normally invokes WER, the Windows Error Reporting program. That offers the user a dialog to send the exception details to Microsoft. Don't expect anything out of that.

通过它的发展远远超出AppDomain.UnhandledException有关管理异常的所有信息都将丢失的时间。没有堆栈跟踪,也不例外消息。只是除了code,你已经知道了,和一个例外地址,你有没有用,因为code是由JIT编译器动态生成的。

By the time it has progressed beyond AppDomain.UnhandledException all info about the managed exception is lost. No stack trace, no exception message. Just the exception code, which you already know, and an exception address, which you'll have no use for because code is dynamically generated by the JIT compiler.

请务必陷阱异常奄奄一息的阶段,编写事件处理程序AppDomain.UnhandledException。登录e.ExcdeptionObject.ToString(的价值),并杀死Environment.Exit程序()。同时提防Application.ThreadException事件Windows窗体code和Dispatcher.UnhandledException事件在WPF code。他们是一个回站,虽然在UI线程处理事件引发的异常。

Be sure to trap the exception in the last gasp stage, write an event handler for AppDomain.UnhandledException. Log the value of e.ExcdeptionObject.ToString() and kill the program with Environment.Exit(). Also beware the Application.ThreadException event in Windows Forms code and Dispatcher.UnhandledException event in WPF code. They are a back stop for exceptions that are raised while processing events on the UI thread.