panic

Keyword that aborts the program with a string message. Writes panic: <msg> and stops the VM; the CLI exits with code 1. Under coil test, a language panic fails the current case only (the next case still runs unless --fail-fast is set).

use io::{stdout};
use io::sync::{write_all};
use string::{format, to_bytes};
panic "unreachable";
panic format("bad index %i", i);

Unlike raise, panic is not recoverable with ? / match. Prefer assert + ? when callers should handle failure.

See examples/panic.hy.

Locations and backtraces#

A panic names the source line it came from, and a runtime error (an out-of-bounds index, a finished coroutine resumed, …) also prints the call stack. Set COIL_BACKTRACE=1 to get the call stack for an explicit panic too:

panic: boom: x too large at panic.hy:3:8
  in inner at panic.hy:3:8
  in outer at panic.hy:9:11
  in main at panic.hy:14:12

How much of this a program can print depends on the debug info it was compiled with (-g0, -g1 or -g2 on coil, coil compile, coil package and coil test):

LevelKeepsA panic prints
-g2function names, lines, byte spansthe output above; coil debug and coil test --coverage need this level
-g1function names, linesthe same output, from a smaller archive
-g0nothingthe message and bare frames

At -g0 the same panic prints:

panic: boom: x too large
  in <unknown>
  ... repeated 2 more times

coil, coil compile and coil test default to -g2; -Os and coil package default to -g1. --strip-debug is the same as -g0. The level never changes what the program does, only what a panic can report. defer blocks still run when a panic unwinds at every level.


↑ ↓ navigate ↵ open