From 14a1cfc892a2cb75e0286ef8e609f24d9d413b49 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Mon, 10 Sep 2018 14:15:22 -0400 Subject: [PATCH] Add error handling example. --- Introduction.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Introduction.md b/Introduction.md index 37a458d..797f338 100644 --- a/Introduction.md +++ b/Introduction.md @@ -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 :)