IT lexicon › Programming › Panic handler

Panic handler

Programming På svenska → Updated: 2026-11-04

The code that runs once a program has reached a state it can't recover from, and which decides what happens next.

In an ordinary Rust binary the standard library prints an error and unwinds the thread. Without it, in no_std, there's nothing to print to and nothing to unwind, so the program must define its own function marked #[panic_handler] that never returns. Ready-made versions come as crates: panic-halt spins in a loop, panic-reset reboots, panic-probe sends the message to the debugger.

The choice is a trade-off. Halting is perfect on the bench, where you can attach a debugger and look around. In the field, a device that just stops is simply broken. The usual compromise is to write the cause to noinit RAM, let the watchdog or a software reset restart things, and read the cause back on the next boot.

← Back to the lexicon