Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2021-08

Raise exception in libray which will not halt host application

7 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

LacaK

Hello,
is there way how to raise exception in library without using SysUtils? Now I call System.RunError() in case unexpected situation occurs in library (DLL), but this exits also application which links library. Is there way how to exit library in a way that application can catch exception (without SysUtils).
If there is Windows call which can be used it is sufficient, as library is Win64 only.
Thanks
-Laco.

Reply by email

Michael Van Canneyt
to LacaK
LacaK wrote
Hello, is there way how to raise exception in library without using SysUtils?

Raise TMyObject.Create()

Where TMyObject does not descend from Exception.

Sysutils is needed for the definition of the Exception class, and because it installs some runtime error to exception conversion hooks, but you can perfectly use exceptions without using the sysutils unit.

Michael.

Reply by email

LacaK
to Michael Van Canneyt

Thank you for both answers!
L.

Show quoted text 17 lines
On Thu, 26 Aug 2021, LacaK via fpc-pascal wrote: > Hello, > is there way how to raise exception in library without using SysUtils? Raise TMyObject.Create() Where TMyObject does not descend from Exception. Sysutils is needed for the definition of the Exception class, and because it installs some runtime error to exception conversion hooks, but you can perfectly use exceptions without using the sysutils unit. Michael.

Reply by email

Sven Barth
to LacaK
LacaK wrote
Thank you for both answers!

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.

Regards,
Sven

Reply by email

LacaK
to Sven Barth
Show quoted text 27 lines
Am 26.08.2021 um 10:10 schrieb LacaK via fpc-pascal: > Thank you for both answers! 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#. 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);

L.

Reply by email

Sven Barth
to LacaK
LacaK wrote 32 lines
> Am 26.08.2021 um 10:10 schrieb LacaK via fpc-pascal: >> Thank you for both answers! > > 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.

Show quoted text 12 lines
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 RaiseException on Windows. 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.

Regards,
Sven

Reply by email

LacaK
to Sven Barth
Show quoted text 30 lines
>> 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.”*

Show quoted text 17 lines
> 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;
Thanks
L.*
*

Reply by email