Raising an exception in a class initializer

Discussions related to Visual Prolog
Martin Meyer
VIP Member
Posts: 271
Joined: 14 Nov 2002 0:01

Raising an exception in a class initializer

Unread post by Martin Meyer » 24 Mar 2019 21:02

Hello Thomas,

please have a look at this code. It raises exception couldNotCreate in a class initializer, it outputs however error r000 (in VIP 900):

Code: Select all

class someClass end class someClass   %---   implement someClass   class facts     flag : boolean := true.   class predicates     initialize : () [classInitializer]. clauses     initialize() :-         if flag = true then             exception::raise_couldNotCreate([])         end if.   end implement someClass   %===   implement main   clauses     run() :-         succeed.   end implement main
Regards Martin

User avatar
Thomas Linder Puls
VIP Member
Posts: 2417
Joined: 28 Feb 2000 0:01

Re: Raising an exception in a class initializer

Unread post by Thomas Linder Puls » 25 Mar 2019 17:21

How does your goal look?
Regards Thomas Linder Puls
PDC

Martin Meyer
VIP Member
Posts: 271
Joined: 14 Nov 2002 0:01

Re: Raising an exception in a class initializer

Unread post by Martin Meyer » 25 Mar 2019 20:12

It is the goal from the console application template.

Code: Select all

goal     console::runUtf8(main::run).
Regards Martin

User avatar
Thomas Linder Puls
VIP Member
Posts: 2417
Joined: 28 Feb 2000 0:01

Re: Raising an exception in a class initializer

Unread post by Thomas Linder Puls » 31 Mar 2019 16:30

Sorry for the late reply, I have been without access to a computer for a while. Anyways, I have reproduced the problem, and we will include the fix in the next release.

Until then you can choose to apply the fix yourself. the clause for mainExe::run/1 should look like this:

Code: Select all

clauses     run(Runnable) :-         OriginalRaiser = programControl::setRuntimeExceptionRaiser(runtime_exception::runtimeExceptionRaiser),         try             runCallback(                 {  :-                     runtime_api::runProgramInitialization(),                     Runnable()                 })         finally             memory::garbageCollect(),             _ = programControl::setRuntimeExceptionRaiser(OriginalRaiser)         end try.
The point is that the program initialization should take place inside the try-catch which is in runCallback.
Regards Thomas Linder Puls
PDC

Martin Meyer
VIP Member
Posts: 271
Joined: 14 Nov 2002 0:01

Re: Raising an exception in a class initializer

Unread post by Martin Meyer » 31 Mar 2019 21:50

Thank you! I have applied the fix and it works fine.
Regards Martin

Post Reply