Add error handling example.

Calvin Rose 2018-09-10 14:15:22 -04:00
parent 6762f9cf87
commit 14a1cfc892
1 changed files with 20 additions and 2 deletions

@ -111,10 +111,10 @@ a table or struct. Note that symbols, keywords and strings are all immutable. Be
code easier to reason about, it allows for many optimizations involving these types.
```lisp
# Prints true
# Evaluates to true
(= :hello :hello)
# Prints false, everything in janet is case sensitive
# Evaluates to false, everything in janet is case sensitive
(= :hello :HeLlO)
# Look up into a table - evaluates to 25
@ -586,6 +586,24 @@ are simply propagated to the next fiber.
(print (resume f)) # -> throws an error because the fiber is dead
```
## Using Fibers to Capture Errors
Besides being used as coroutines, fibers can be used to implement error handling (exceptions).
```lisp
(defn my-function-that-errors [x]
(print "start function with " x)
(error "oops!")
(print "never gets here"))
# Use the :e flag to only trap errors.
(def f (fiber.new my-function-that-errors :e))
(def result (resume f))
(if (= (fiber.status f) :error)
(print "result contains the error")
(print "result contains the good result"))
```
# Macros
:)