Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch exceptions in initialization section of Units in Delphi

I have a third party unit, witch raises an exception in initialization section of unit. How can I catch this exception on my application?

like image 553
Reza Avatar asked Nov 11 '22 06:11

Reza


1 Answers

You cannot catch such a thing. The RTL executes the initialization sections and the rules are that no exceptions are to be raised. If exceptions are raised, then the fault is terminal.

Another way to thing about this is that when the initialization sections start to be executed, the language exception handling framework is not yet in place. That itself is installed as part of the RTL initialization.

The solution is to fix the code so that it obeys the rules. No exceptions raised in initialization sections.


Well, I suppose that you could hook the RTL code that executes initialization and perhaps replace that RTL code with exception resilient code. But what would be the point? If an initialization section raises an exception, the only sane assumption to be made is that the unit is not initialized and so not fit for use. Please don't try to bury your head in the sand and ignore the real problem. Fix the third party code.

like image 128
David Heffernan Avatar answered Nov 15 '22 05:11

David Heffernan