Skip to content

RecursionError

The error for a call that never comes back. A function that calls itself — a walk over a tree, a parser over nested text — is stopped once the calls are stacked deeper than any working program needs, rather than being left to exhaust the machine and take the whole process with it.

It sits under RuntimeError, and it is an ordinary one: a catch takes it, and by the time the handler runs the deep calls are gone, so the program carries on with the room it started with. That is what makes a recursive walk over data somebody else supplied safe to attempt — the bad case costs one request, not the process.

Raising one yourself is often better still. Your own limit — three levels of a menu, thirty of a reply thread — is reached long before the language's, and the message can name what was too deep.

The examples below show a thread whose replies point back at themselves, a menu with a depth budget of its own, and an import that drops the one feed it cannot finish and keeps going.

Examples

A thread that is its own reply

<?nvs
// A comment thread is drawn by walking each reply's own replies. If the stored
// data says a reply sits below itself, that walk never ends. Novis stops it,
// and the page can say what went wrong instead of dying.
class Reply
{
public string $author;
public array<Reply> $replies;
public function constructor(string $author)
{
$this->author = $author;
$this->replies = [];
}
public function add(Reply $reply): void
{
$this->replies[] = $reply;
}
}
class Thread
{
public static function count(Reply $reply): uint
{
uint $total = 1;
foreach ($reply->replies as Reply $child) {
$total = $total + Thread::count($child);
}
return $total;
}
}
var $first = new Reply('mina');
var $second = new Reply('sol');
$first->add($second);
// The row that should never have been written: the first reply is listed again
// underneath its own reply.
$second->add($first);
try {
uint $drawn = Thread::count($first);
echo 'replies: ', $drawn, "\n";
} catch (RecursionError $loop) {
echo 'cannot draw the thread: ', $loop->message, "\n";
}
echo "the page is still served\n";
Output
cannot draw the thread: the call stack is too deep
the page is still served

A depth limit of your own

<?nvs
// The language stops a runaway call far deeper than most code should ever go.
// Where you know the real limit, say so yourself: the error then names what was
// too deep, and it arrives while everything is still in a state you can report.
class Menu
{
public string $label;
public array<Menu> $items;
public function constructor(string $label)
{
$this->label = $label;
$this->items = [];
}
public function add(Menu $item): void
{
$this->items[] = $item;
}
}
class Render
{
public static function lines(Menu $menu, uint $depth): uint
{
// Three levels is what the design allows. A menu nested deeper is a
// mistake in the shop's settings, not something to draw.
if ($depth > 3) {
throw new RecursionError('the menu is nested deeper than three levels');
}
echo Core\Str::repeat(' ', $depth), $menu->label, "\n";
uint $drawn = 1;
foreach ($menu->items as Menu $item) {
$drawn = $drawn + Render::lines($item, $depth + 1);
}
return $drawn;
}
}
var $shop = new Menu('Shop');
var $clothes = new Menu('Clothes');
var $coats = new Menu('Coats');
var $winter = new Menu('Winter');
var $wool = new Menu('Wool');
$shop->add($clothes);
$clothes->add($coats);
$coats->add($winter);
$winter->add($wool);
try {
echo Render::lines($shop, 0), " entries drawn\n";
} catch (RecursionError $deep) {
echo 'menu not drawn: ', $deep->message, "\n";
}
Output
Shop
Clothes
Coats
Winter
menu not drawn: the menu is nested deeper than three levels

A feed whose pages never end

<?nvs
// An importer walks every page of a feed by calling itself with the cursor the
// last page handed back. A feed whose cursor never runs out would keep the
// importer going until the machine gave out. Instead the call is stopped, that
// feed is written off, and the next one is still imported.
class Feed
{
// Stands in for the call to the supplier: it answers with the next cursor,
// or with zero once the feed is finished.
public static function next(string $name, uint $cursor): uint
{
if ($name == 'orders' && $cursor == 3) {
return 0;
}
return $cursor + 1;
}
public static function import(string $name, uint $cursor): uint
{
uint $next = Feed::next($name, $cursor);
if ($next == 0) {
return 1;
}
return 1 + Feed::import($name, $next);
}
}
array<string> $feeds = ['orders', 'prices'];
foreach ($feeds as string $name) {
try {
uint $pages = Feed::import($name, 0);
echo $name, ': ', $pages, " pages imported\n";
} catch (RecursionError $endless) {
// Worth an alert to whoever owns the supplier, and no reason to stop.
echo $name, ": the pages never end, so nothing was imported\n";
}
}
echo "the run finished\n";
Output
orders: 4 pages imported
prices: the pages never end, so nothing was imported
the run finished