Exceptions in CLIPS

67 views
Skip to first unread message

Руслан Сорокин

unread,
Nov 27, 2024, 2:28:07 AM11/27/24
to CLIPSESG
Dear CLIPS users and team!

Is there in CLIPS possibility to handle errors like the exception mechanism in other languages? More specifically, how to continue execution after an error programmatically?
Any tips and triks would be greatly appreciated.

Sincerely,
  Ru

CLIPS Support

unread,
Dec 1, 2024, 3:05:03 PM12/1/24
to CLIPSESG
While there's nothing sophisticated like try/catch functionality, you can use the set-error/get-error functions to assign error values that can be propagated back up through a chain of function calls independent of the return values of the function calls.

         CLIPS (6.4.1 4/8/23)
CLIPS>
(deffunction divide (?dividend ?divisor)
   (clear-error)
   (if (not (numberp ?dividend))
       then
       (set-error non-numeric-dividend)
       (return NaN))
   (if (not (numberp ?divisor))
       then
       (set-error non-numeric-divisor)
       (return NaN))
   (if (= ?divisor 0)
       then
       (set-error divide-by-zero)
       (return NaN))
   (/ ?dividend ?divisor))
CLIPS> (divide 3 a)
NaN
CLIPS> (get-error)
non-numeric-divisor
CLIPS> (divide 3 0)
NaN
CLIPS> (get-error)
divide-by-zero
CLIPS> (divide 4 2)
2.0
CLIPS> (get-error)
FALSE
CLIPS> 


Руслан Сорокин

unread,
Dec 2, 2024, 2:58:24 AM12/2/24
to CLIPSESG
This is a great solution. Thanks. I will use it.
But it is only suitable for those cases that I can foresee and determine in advance.
But this is not always the case with the errors listed in p. Appendix F of Basic Programming Guide.
It would be cool to have a function that can handle any error.

воскресенье, 1 декабря 2024 г. в 23:05:03 UTC+3, CLIPS Support:

CLIPS Support

unread,
Dec 2, 2024, 7:15:54 PM12/2/24
to CLIPSESG
Attached is a very simple try/catch user-defined function. You won't be able to tell what error occurred or prevent the error message from being displayed, but you can detect that an error has occurred and allow execution to continue. 

CLIPS> 
(loop-for-count (?i 3)
   (try
      (println "Before " ?i)
      (println (/ 4 (- ?i 2)))
      (println "After " ?i)
    catch
      (print "Continue? ")
      (if (neq (read) yes) then (break))))
Before 1
-4.0
After 1
Before 2
[PRNTUTIL7] Attempt to divide by zero in '/' function.

Continue? yes
Before 3
4.0
After 3
FALSE
CLIPS>
(loop-for-count (?i 3)
   (try
      (println "Before " ?i)
      (println (/ 4 (- ?i 2)))
      (println "After " ?i)
    catch
      (print "Continue? ")
      (if (neq (read) yes) then (break))))
Before 1
-4.0
After 1
Before 2
[PRNTUTIL7] Attempt to divide by zero in '/' function.

Continue? no
FALSE
CLIPS> 


userfunctions.c

Руслан Сорокин

unread,
Dec 5, 2024, 2:54:51 AM12/5/24
to CLIPSESG
Excellent!
That's just what I wanted. Thank you very much, Gary!
Maybe it should be included in future versions of CLIPS?
Sincerely,
  Ru

вторник, 3 декабря 2024 г. в 03:15:54 UTC+3, CLIPS Support:

CLIPS Support

unread,
Dec 6, 2024, 11:25:11 AM12/6/24
to CLIPSESG
I'll include it in a future release.

Руслан Сорокин

unread,
Dec 9, 2024, 8:43:38 AM12/9/24
to CLIPSESG
Great! Thank you once more.
When can we expect the next release? At least approximately? Will it be 7.0.0?
Thank you once more.
Sincerely,
   Ru

пятница, 6 декабря 2024 г. в 19:25:11 UTC+3, CLIPS Support:

CLIPS Support

unread,
Dec 11, 2024, 12:24:33 PM12/11/24
to CLIPSESG
I'm going to try to put together a 6.4.2 release at the beginning of 2025 and then try to finish up 7.0 for a beta release after that.

Руслан Сорокин

unread,
Dec 11, 2024, 1:22:10 PM12/11/24
to CLIPSESG
Thank you! Good luck with your work.
Best regards,
  Ru 

среда, 11 декабря 2024 г. в 20:24:33 UTC+3, CLIPS Support:
Reply all
Reply to author
Forward
0 new messages