[fpc-pascal] Raise exception in libray which will not halt host application

LacaK lacak at zoznam.sk
Thu Aug 26 13:39:36 CEST 2021

>>> Please note that you won't be able to catch such an exception with 
>>> the type from within the application, e.g.:
>>> === code begin ===
>>> try
>>>   SomeLibraryFunction;
>>> except
>>>   on e: TMyObject do
>>>      Whatever;
>>>   else
>>>      WhateverElse;
>>> end;
>>> === code end ===
>>> The above code would go to the first branch *inside* your library, 
>>> but it will *always* go to the else-branch outside your library, 
>>> cause the comparison is done on the pointer to the VMT (the class 
>>> type) which will be *different* between library and application 
>>> (even if you use the same unit!). This is one of the main reasons 
>>> why dynamic packages are needed for comfortable use of libraries in 
>>> Object Pascal, because it allows such types to be shared across 
>>> module boundaries.
>> My situation is even worser :-)
>> Library DLL is writen in Pascal, but main application is C/C#.
> Then it will probably be caught as some external exception or such... 
> don't know, never tried it.
*“System.Runtime.InteropServices.SEHException (0x80004005): External 
component has thrown an exception.”*

>> When I call in library RunError() then whole application aborts.
>> I need throw exception in library which can be catched in application.
>> I now use in unit which is used by library:
>>   if System.IsLibrary then
>>     raise TObject.Create // raise exception - type of exception is 
>> not important ...
>>     // can I call here also Windows's RaiseException()? ... so it 
>> will be propagated to linking application?
>>   else
>>     System.RunError(w);
> "raise" internally uses . The only difference would be that you'd have 
> a bit more control of exception code and such if you want to ensure 
> that it's caught nicely by the C/C++/C# code.

Ok, then if raise TObject.Create; calls RaiseException then I think that 
I will use raise TObject.Create;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20210826/01c576a0/attachment.htm>

More information about the fpc-pascal mailing list