Re: Handling unhandled exceptions
- From: Jeroen Mostert <jmostert@xxxxxxxxx>
- Date: Fri, 07 Mar 2008 01:54:12 +0100
Bob Altman wrote:
I've written a DLL in "interop" mode (compiled with /clr). This DLL is a wrapper for a managed library. It exposes native C-style entry points, calls into the managed library, and marshals the results (if any) back to the native caller.Actually, in addition to what others have said, this is not your responsibility. It is the caller's responsibility in turn (and ultimately whoever's responsible for the process as a whole) to handle unhandled exceptions from *your* code. If the caller is/might be a pure C caller that doesn't know about exceptions at all, then it's your job to make sure no exceptions are ever propagated from your functions (at least not those that can be adequately captured in error codes), so exception handlers around every call are entirely appropriate. Otherwise, propagating them may be acceptable, and your client will have to worry about them.
Here's the short version of the question: Is there an easy way for my library to catch unhandled managed exceptions (short of putting a try/catch block around each top-level routine in my library)? As I explain below, I tried wiring up the AppDomain.CurrentDomain.UnhandledException event, but that didn't work.
Here's the longer story:
If the managed code called by my library throws an exception that is ultimately unhandled then my library's caller dies a horrible death. That horrible death includes a dialog box that basically tells the user that the program died, but offers little by way of useful information about unhandled exception (like the exception name and the call stack). I want to display my own custom dialog in response to an unhandled exception (before I kill the caller).
This is also why throwing up a dialog and killing the caller is something you shouldn't do even if you could; the caller may have its own ideas about how to handle exceptions from your library. Only if your library is calling the shots (or is expected to call the shots) could you do something like muck around with .UnhandledException.
If you absolutely, positively must have this level of control, you could always make your DLL purely unmanaged and host the CLR yourself. This will allow you to customize exception handling to your heart's content, but it's not something you should do lightly (for one thing, this will simply not work if your DLL is ever used in a context where the CLR is already hosted, and it obviously makes interacting with managed code a lot less convenient).
--
J.
.
- References:
- Handling unhandled exceptions
- From: Bob Altman
- Handling unhandled exceptions
- Prev by Date: Re: about goto
- Next by Date: RE: Handling unhandled exceptions
- Previous by thread: Re: Handling unhandled exceptions
- Next by thread: RE: Handling unhandled exceptions
- Index(es):
Relevant Pages
|