Exceptions and DLL in Delphi

后端 未结 4 1957
心在旅途
心在旅途 2020-12-19 13:05

What is the right way to handle exceptions thrown from inside a DLL in Delphi?

Something like this

on E : ESomeException do ...

or<

4条回答
  •  难免孤独
    2020-12-19 13:38

    For pure DLL's exceptions are not allowed to cross the DLL boundary (like Deltics mentions) - no matter what language.

    You get all sorts of trouble there, especially because you don't know which language, RTL, memory manager, etc, is on each side of the boundary.

    So you are back to the classic error handling paradigm:

    • error codes (similar to HResult)
    • error messages (similar to GetLastError)

    Instead of DLL's, you could use BPL packages (as Lars suggested): there you know that both sides will use the same RTL and memory manager.

    Both packages and BPL usually give you a versioning nightmare anyway (too many degrees of freedom).

    A more rigorous solution is to go for a monolithic executable; this solves both problems:

    • much easier versioning
    • guaranteed only one RTL and memory manager

    --jeroen

    PS: I've made this an additional answer because that allows for easier pasting of links.

提交回复
热议问题