Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch absolutely all exceptions / errors

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

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

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

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".

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.

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

like image 900
chriszero Avatar asked May 07 '11 09:05

chriszero


People also ask

How do you catch all exceptions?

Master C and Embedded C Programming- Learn as you go Exception handling is used to handle the exceptions. We can use try catch block to protect the code. Catch block is used to catch all types of exception. The keyword “catch” is used to catch exceptions.

How do you catch all errors in Python?

Another way to catch all Python exceptions when it occurs during runtime is to use the raise keyword. It is a manual process wherein you can optionally pass values to the exception to clarify the reason why it was raised. if x <= 0: raise ValueError(“It is not a positive number!”)

Does exception catch all exceptions?

yeah. Since Exception is the base class of all exceptions, it will catch any exception.

Which level of error handling is handler catches all exceptions?

Page Level Error Event Handling This code example shows a handler for the Error event in an ASP.NET Web page. This handler catches all exceptions that are not already handled within try / catch blocks in the page.


2 Answers

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.

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.

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

like image 197
sehe Avatar answered Oct 08 '22 15:10

sehe


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.

like image 42
Matti Virkkunen Avatar answered Oct 08 '22 13:10

Matti Virkkunen