Panic.raise
func Panic.raise(p: Panic): neverPanic.raise panics with p's message and trace as they are, so a caught panic goes on as though it had not been caught.
Details
- The panic keeps its original trace, so the report and any outer Panic.catch point to where the panic began, not to the
Panic.raisecall. Panic.raisedoes not return. Afterif r is Err { Panic.raise(r.error) }, the checker treatsras anOk.
Basic examples
Raise a panic, and catch it to see the record it carries:
let p = Panic{ message: "boom", trace: [] }
Panic.raise(p) // panic: boom
Panic.catch({ Panic.raise(p) }) // Err{ error: Panic{ message: "boom", trace: [] } }Catch a panic, log it, and let it go on:
func parse(s: string): int {
if s == "" { panic("empty input") }
s.length()
}
let r = Panic.catch({ parse("") })
if r is Err {
println("caught: ${r.error.message}")
Panic.raise(r.error)
}
println("not reached")caught: empty inputScope
Call it through .:
let p = Panic{ message: "stop", trace: [] }
p.raise() // panic: stopA caught panic from the runtime, such as an index out of range, raises with its own message and trace. The report points to the line that indexed, not to the line that raised:
let r = Panic.catch({ [1, 2][5] }) // panic: index 5 out of range for an array of length 2
if r is Err { Panic.raise(r.error) }A trace is plain data. A panic built by hand carries the frames you write, and a catch sees them unchanged:
let frame = Panic.Frame{ function: "load", path: "lib.ant", line: 3, column: 5 }
let p = Panic{ message: "x", trace: [frame] }
let r = Panic.catch({ Panic.raise(p) })
assert(r is Err)
r.error.trace // [Panic.Frame{ function: "load", path: "lib.ant", line: 3, column: 5 }]A call of Panic.raise has type never, so it fits where a value is expected, such as a branch of an if expression:
func valueOf(r: Result[int, Panic]): int {
let v = if r is Ok { r.value } else { Panic.raise(r.error) }
v + 1
}
println(valueOf(Result.ok(1)))2A raised panic in a goroutine ends that goroutine only, as any panic does:
let wg = WaitGroup.new()
wg.add(1)
go {
defer { wg.done() }
let r = Panic.catch({ [1, 2][5] })
if r is Err { Panic.raise(r.error) }
}
wg.wait()
println("main goes on")main goes onApplications
Run every job, even when one panics, and still fail at the end with the first panic and its original trace:
func handle(job: string): int {
if job == "" { panic("empty job name") }
job.length()
}
let failures: Array[Panic] = []
for job in ["build", "", "deploy"] {
let r = Panic.catch({ handle(job) })
if r is Err {
println("job \"${job}\": ${r.error.message}")
failures.push(r.error)
} else {
println("job \"${job}\": ${r.value}")
}
}
if failures.length() > 0 { Panic.raise(failures[0]) }job "build": 5
job "": empty job name
job "deploy": 6Add context to the message and keep the trace, with a spread:
func load(): int {
let xs = [1]
xs[4] // panic: loading config: index 4 out of range for an array of length 1
}
let r = Panic.catch(load)
if r is Err {
Panic.raise(Panic{ ...r.error, message: "loading config: ${r.error.message}" })
}Clean up inside the caught function with defer, which runs before Panic.catch returns, then pass the panic on:
func step(name: string) {
let r = Panic.catch({
defer { println("cleanup ${name}") }
panic("${name} failed")
})
if r is Err {
println("log: ${r.error.message}")
Panic.raise(r.error)
}
}
step("upload")cleanup upload
log: upload failedProperties and relations
Catching a raised panic gives back an equal panic:
let first = Panic.catch({ [1][3] })
if first is Err {
let again = Panic.catch({ Panic.raise(first.error) })
assert(again is Err && again.error == first.error)
}Possible issues
panic(r.error.message) passes the message on but starts a new trace, so the report points to the line that panicked again and the original location is lost:
let r = Panic.catch({ [1][4] })
if r is Err {
panic(r.error.message) // panic: index 4 out of range for an array of length 1
}Use Panic.raise(r.error) to keep the trace.
Panic.raise takes the Panic, not the Result that holds it:
let r = Panic.catch({ 21 * 2 })
if r is Err { Panic.raise(r) } // error: cannot use Err[Panic] where Panic is expectedSee also
Panic.catch · Panic.Frame · panic · assert · defer · Errors guide
