mirror of
https://github.com/janet-lang/janet
synced 2025-01-14 09:25:41 +00:00
Add error handling example.
parent
6762f9cf87
commit
14a1cfc892
@ -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
|
||||
|
||||
:)
|
||||
|
Loading…
Reference in New Issue
Block a user