💾 Archived View for thebird.nl › gn-gemtext-threads › topics › lisp › define-condition.gmi captured on 2023-04-19 at 23:36:30. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2022-04-28)

-=-=-=-=-=-=-

Common Lisp Condition Handling

One of Lisp's great features is its condition system.
Every Common Lisp implementation has a condition system.
... conditions are more general than exceptions in that a condition can
represent any occurrence during a program's execution that may be of
interest to code at different levels on the call stack.

The following three quotes are abridged from a conversation with

Ed Langley over #common-lisp:matrix.org (Thu Dec 23 07:46:30 PM EST 2021)

Ed Langley's GitHub

Why is condition handling special in common lisp compared to other languages?

Because code can define recovery strategies without handling the condition.
Any bit of code can be wrapped with restart-case to define a
restart. This restart defines a way you could continue from any condition
that’is signaled within the dynamic extent of the restart-case.
Your condition handler can do something like (find-restart ‘continue
c) to find the CONTINUE restart associated with condition C. If the
restart exists, it can use invoke-restart to jump to the restart which
runs and then execution continues like normal after the restart-case form.

-- end of coversation

Restarts, restarts, restarts: interactive choices in the debugger

Restarts are the choices we get in the debugger, which always has
the RETRY and ABORT ones. By handling restarts we can start over the
operation as if the error didn't occur (as seen in the stack).
(divide 3 0)
;; Y can not be zero. Please change it
;;    [Condition of type SIMPLE-ERROR]
;;
;; Restarts:
;;  0: [CONTINUE] Retry assertion with new value for Y.  <--- new restart
;;  1: [RETRY] Retry SLIME REPL evaluation request.
;;  …

Can the condition system be used in both production and for debugging?

both, production code and debugging. Every fault that you handle manually
using the debugger can be written down into some Lisp code; put that code
inside a handler function and wrap a handler-bind around your toplevel and
bam, you have achieved automatic error handling with a bit of clever
design. You never even need to kill your program to add new handlers. It
can simply keep on running™

- phoe (#common-lisp:matrix.org, feb 10)

Video References

Common Lisp Study Group: Conditions and Restarts

Common Lisp Tutorial 5a: Condition System

Common Lisp Tutorial 5b: Condition System Part 2

Integrating independent condition systems

Condition Systems in an Exceptional Language

Little bits of lisp - Inspecting a condition

Immutable Conversations

Internet, Blog, Book, and Article References

https://devpoga.org/post/2021-04-25_common_lisp_condition_system/

find-restart

define-condition in the cl-cookbook

Condition Handling in the Lisp Language Family

Conditions and Restarts

Tags