- VM refactor: resumable CAT_VM_Frame (ip/sp/locals/handlers) - yield/next/generators: calling a yield-function returns a generator - for..in over any iterator, ends on explicit exhaustion status - raise/try/except: structured error values, nearest-handler unwinding - slots: C-declared member layout (offsetof), PyTypeObject/tp_members style - File now exposes path/mode/closed via slots - examples: generators.cat, errors.cat, slots.cat; 40/40 tests
39 lines
609 B
Plaintext
39 lines
609 B
Plaintext
-- examples/errors.cat -- raise, try, except.
|
|
|
|
function risky(n)
|
|
if n < 0 then
|
|
raise "negative not allowed"
|
|
end
|
|
return n * 2
|
|
end
|
|
|
|
-- raise is caught by try/except
|
|
try
|
|
print("ok:", risky(21))
|
|
raise "boom"
|
|
print("unreachable")
|
|
except e
|
|
print("caught:", e)
|
|
end
|
|
|
|
-- a structured (non-string) error value can be caught too
|
|
try
|
|
raise { code = 42, what = "custom" }
|
|
except e
|
|
print("code:", e.code)
|
|
print("what:", e.what)
|
|
end
|
|
|
|
-- errors propagate out of nested calls to the nearest handler
|
|
function outer()
|
|
return risky(-1)
|
|
end
|
|
|
|
try
|
|
outer()
|
|
except e
|
|
print("propagated:", e)
|
|
end
|
|
|
|
print("still running")
|