I am working in a DLL which sometimes raises unhandled exceptions. I am using madExcept to detect and debug the buggy code, but when I finally deploy my DLL I want include my own global exception handler inside the DLL to log the exceptions.
So the question is how I can set a global exception handler in my Delphi DLL?
The concept of “a global exception handler” doesn’t really exist in DLLs the way it exists in the VCL. To understand why, remember that exceptions propagate by unwinding the stack until they can find a handler. The VCL can install a global exception handler because in a VCL app, everything that happens (excluding startup and shutdown) will have
TApplication.Runsomewhere in the call stack, and that’s where it puts the exception handler. Since your DLL doesn’t have a single central point like that, you can’t do it that way.What you can do is set up a “central exception handler routine” in your DLL somewhere. It should take an
Exceptionobject as a parameter. Then do something like this for all your exported routines:That’s really the best you can do, unless you’re using COM. If you’re writing a COM DLL, mark your interface methods with the
safecallcalling convention and the compiler will silently generate code for you that takes care of exception propagation.