Chapter II · Idioms & deeper language / Lesson 13 of 27

Errors, pcall, xpcall

You'll learn: Raise errors as strings *or* tables, catch them with pcall, and add a handler with xpcall.

error(value) raises — the value is usually a string but a table works and is great for structured failures. pcall(f, …) returns (true, ret…) or (false, err). xpcall(f, handler, …) lets you attach context (e.g. a traceback) *before* the stack unwinds.

errors.lua
Output idle
Hit Run to execute.
Try it: Replace handler with one that returns { wrapped = true, original = tostring(e) } and read wrapped.wrapped from the caller side.