RuntimeError
The error a program raises when something outside it said no.
A file that has gone, a document that arrived malformed, a database that gave up, a wait that ran out: the code is right and the world did not cooperate. Novis gives that its own half of the exception tree, with narrower names underneath — IOError, ParseError, TimeoutError, RecursionError and the ones the database and the terminal raise — so a program can catch the whole half in one clause or pick out the one kind it knows how to answer.
This is the half worth catching. A failure from out here is usually worth retrying, reporting or falling back on, where the same treatment applied to a bug in the code would just repeat it.
Good to know: it sits beside LogicError under Throwable, so a RuntimeError clause never takes a mistake the program made in its own call.
The examples below show a service raising one, one clause standing in for every narrower kind, and a retry that knows which failures are worth a second attempt.
Examples
When the world says no
<?nvs// This program throws a `RuntimeError` when something outside the program fails.// The rate feed has no rate for `SEK`. The code cannot fix that,// so it throws the error and the caller catches it.
final class Rates{ public static function forCurrency(string $code): int { array<string> $known = ['EUR', 'GBP', 'CHF']; array<int> $perThousand = [1000, 1183, 1067]; int $row = 0; while ($row < 3) { if ($known[$row] == $code) { return $perThousand[$row]; } $row = $row + 1; } throw new RuntimeError('no rate for ' . $code . " in today's feed"); }}
array<string> $wanted = ['GBP', 'SEK'];foreach ($wanted as string $code) { try { int $rate = Rates::forCurrency($code); echo $code, ': ', $rate, "\n"; } catch (RuntimeError $noRate) { echo $code, ': ', $noRate->message, "\n"; }}GBP: 1183SEK: no rate for SEK in today's feedOne clause for every narrower kind
<?nvs// Under this name sit the narrower ones: a file that failed, a document that// would not parse, a wait that ran out. A handler that treats them all the same// way writes one clause and catches the lot, and the binding still says which// kind actually arrived.
final class Intake{ public static function step(string $stage): tainted string { try { if ($stage == 'upload') { throw new IOError('the upload was cut off'); } if ($stage == 'document') { var $rows = Core\Json::decode('[1, 2,'); return 'parsed'; } if ($stage == 'provider') { throw new TimeoutError('the provider took too long'); } return 'done'; } catch (RuntimeError $failed) { if ($failed is ParseError) { return 'rejected: the file was not valid JSON'; } return 'retry later: ' . $failed->message; } }}
echo Intake::step('upload'), "\n";echo Intake::step('document'), "\n";echo Intake::step('provider'), "\n";echo Intake::step('finish'), "\n";retry later: the upload was cut offrejected: the file was not valid JSONretry later: the provider took too longdoneA retry that knows what to retry
<?nvs// The reason the tree splits where it does. A failure from outside is often// worth another attempt; a wrong call is not, because the second attempt makes// exactly the same wrong call. So the retry loop catches one half and lets the// other straight through.
final class Delivery{ // Fails twice the way a flaky endpoint does, then succeeds. The fourth // parcel is addressed with a number this code should never have passed on. public static function send(string $parcel, int $attempt): string { if ($parcel == 'flaky' && $attempt < 3) { throw new IOError('the endpoint closed the connection'); } if ($parcel == 'misaddressed') { var $label = Core\BigInt::of(6042)->format({ radix: 1 }); return 'labelled ' . $label; } return 'accepted'; }
public static function withRetries(string $parcel): tainted string { int $attempt = 1; while ($attempt <= 3) { try { return Delivery::send($parcel, $attempt) . ' on attempt ' . ($attempt as string); } catch (RuntimeError $outside) { if ($attempt == 3) { return 'gave up: ' . $outside->message; } } $attempt = $attempt + 1; } return 'unreachable'; }}
echo 'steady: ', Delivery::withRetries('steady'), "\n";echo 'flaky: ', Delivery::withRetries('flaky'), "\n";
// The bug is not retried. It leaves the loop on the first attempt and reaches// whoever is responsible for fixing it.try { tainted string $labelled = Delivery::withRetries('misaddressed'); echo 'misaddressed: ', $labelled, "\n";} catch (LogicError $ourBug) { echo 'misaddressed: not retried, ', $ourBug->message, "\n";}steady: accepted on attempt 1flaky: accepted on attempt 3misaddressed: not retried, Core\BigInt::format(): radix 1 is outside the 2 to 36 this class reads and writes