Skip to content

Instantly share code, notes, and snippets.

@juan-reynoso
Created November 17, 2022 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juan-reynoso/a10d5b6e885c6aa2c7fdfd43561c79cf to your computer and use it in GitHub Desktop.
Save juan-reynoso/a10d5b6e885c6aa2c7fdfd43561c79cf to your computer and use it in GitHub Desktop.
The basic form of a HANDLER-CASE
;;;; Condition Handler
;;;; The macro HANDLER-CASE establishes this kind of condition handler.
;;;; The basic form of a HANDLER-CASE is as follows:
;;;; (handler-case expression
;;;; error-clause*)
;;;; where each error-clause is of the following form:
;;;; (condition-type ([var]) code)
(defun my-division (x y)
(handler-case
;; The body of a HANDLER-CASE must be a single expression; you
;; can use PROGN to combine several expressions into a single form
;; Run this code
;; It is like try in (Python, Java and C++)
(/ x y)
;; Condition type
;; It will cath any type of condition
(error ()
;; It is like except in Python
;; It is like catch in Java or C++
;; Execute this code when there is an condition
(format t "Something went wrong."))))
(defun my-division-v2 (x y)
(handler-case
;; The body of a HANDLER-CASE must be a single expression; you
;; can use PROGN to combine several expressions into a single form
;; Run this code
(/ x y)
;; Condition type
;; Execute this code when there is a condition equal to 'division-by-zero
(division-by-zero ()
(format t "You can't divide by zero!" ))))
(defun my-division-v3 (x y)
(handler-case
;; The body of a HANDLER-CASE must be a single expression; you
;; can use PROGN to combine several expressions into a single form
;; Execute this code
(/ x y)
;; Condition type
;; Execute this code when there is a condition equal to 'division-by-zero
;; The condition is signaled
(division-by-zero ()
(format t "You can't divide by zero!" ))
;; Condition type
;; Execute this code when there is a condition equal to 'type-error
(type-error ()
(format t "Both of them must be numbers!" ))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment