如何捕获所有异常/错误

时间:2022-10-28 10:47:52

I have a windows service application, running under WinXPe, which sometimes fails with an error and displays an message box to the user:

我有一个在WinXPe下运行的windows服务应用程序,它有时会出错,并向用户显示一个消息框:

"The instruction at “” referenced memory at “0x00000000”. The memory could not be “read.” Press OK to exit the program

“在”“0x00000000”中引用内存的指令。记忆不能被“读取”。按OK退出程序

If the user clicks "Ok" the service is restarting.

如果用户单击“Ok”,服务将重新启动。

I have tried to catch all unhandled exceptions with registering a eventhandler at AppDomain.CurrentDomain.UnhandledException in the handler I log the exception details and exit the application. But the error I mentioned above is NOT handled from "UnhandledException".

我尝试通过在AppDomain.CurrentDomain注册一个eventhandler来捕获所有未处理的异常。在处理程序中UnhandledException异常记录异常细节并退出应用程序。但是我上面提到的错误不是通过“UnhandledException”处理的。

The application is heavily multi threaded, using System.Threading.Timer and System.Threading.Thread. And it's using some third party libs, one of these libs are using native interop, I have no source of the native lib.

该应用程序使用System.Threading来实现多线程。计时器和System.Threading.Thread。它使用的是第三方的libs,其中一个libs使用的是native - interop,我没有本地lib的源。

I tried to point out the error with an debugger attached, but the error doesn't show up ;) The application has to run several days before the error occurs.

我试图用附加调试器指出错误,但是错误没有出现;应用程序必须在错误发生前运行几天。

I need a way to handle such a error.

我需要一个方法来处理这样的错误。

Thanks

谢谢

2 个解决方案

#1


3  

See Vectored Exception Handling

看到矢量异常处理

This is part of windows SEH (Structured Exception Handling) and IIRC here is precious few errors that you could not at least be notified of in such a case.

这是windows SEH(结构化异常处理)和IIRC的一部分,在这种情况下,很少有错误是不能被通知的。

You will probably want to write any handling code directly to the native WIN32 API (in unsafe/unmanaged code) and using pre-allocated (static?) buffers only, because there will be many things unreliable at that moment in time.

您可能希望直接向本机WIN32 API(不安全/非托管代码)编写任何处理代码,并且只使用预分配(静态?)缓冲区,因为此时会有许多东西不可靠。

Beware of/stay away from threading, locking primitives, memory allocations, disk IO; preferrably use Windows default API's to, e.g. restart the process or produce a minidump and things like that

注意/远离线程、锁定原语、内存分配、磁盘IO;最好使用Windows默认API的to,例如重新启动进程或生成一个minidump等等

#2


3  

That error is not a managed exception. It's a lower level memory access violation. Essentially a NULL pointer access in native code.

这个错误不是托管异常。这是较低级别的内存访问违例。本质上是本地代码中的空指针访问。

This is something you're supposed to be completely protect from in managed code, so it's likely one of your native libraries or the way you're using them. If the error only appears after a few days of execution, you might be best off first going through any native library calls, checking their signatures and making sure you pass them data that makes sense.

这是您应该在托管代码中完全保护的内容,因此它很可能是您的本地库或您使用它们的方式之一。如果错误只在执行几天后出现,那么您最好首先检查任何本机库调用,检查它们的签名,并确保传递给它们有意义的数据。

#1


3  

See Vectored Exception Handling

看到矢量异常处理

This is part of windows SEH (Structured Exception Handling) and IIRC here is precious few errors that you could not at least be notified of in such a case.

这是windows SEH(结构化异常处理)和IIRC的一部分,在这种情况下,很少有错误是不能被通知的。

You will probably want to write any handling code directly to the native WIN32 API (in unsafe/unmanaged code) and using pre-allocated (static?) buffers only, because there will be many things unreliable at that moment in time.

您可能希望直接向本机WIN32 API(不安全/非托管代码)编写任何处理代码,并且只使用预分配(静态?)缓冲区,因为此时会有许多东西不可靠。

Beware of/stay away from threading, locking primitives, memory allocations, disk IO; preferrably use Windows default API's to, e.g. restart the process or produce a minidump and things like that

注意/远离线程、锁定原语、内存分配、磁盘IO;最好使用Windows默认API的to,例如重新启动进程或生成一个minidump等等

#2


3  

That error is not a managed exception. It's a lower level memory access violation. Essentially a NULL pointer access in native code.

这个错误不是托管异常。这是较低级别的内存访问违例。本质上是本地代码中的空指针访问。

This is something you're supposed to be completely protect from in managed code, so it's likely one of your native libraries or the way you're using them. If the error only appears after a few days of execution, you might be best off first going through any native library calls, checking their signatures and making sure you pass them data that makes sense.

这是您应该在托管代码中完全保护的内容,因此它很可能是您的本地库或您使用它们的方式之一。如果错误只在执行几天后出现,那么您最好首先检查任何本机库调用,检查它们的签名,并确保传递给它们有意义的数据。