Skip to content

Core\Script\Finished

Every script that ends early ends through this class. Core\Script::finish() raises it, and what travels out is an ordinary raised value, so leaving takes the same path a thrown error takes: every finally between the call and the top of the program runs, and what was opened is closed on the way out. Nothing after the call runs.

What it is not is a failure. No catch takes it — not even one written for Throwable — because it sits outside the error tree altogether rather than being excluded case by case. So the wrapper that reports errors never sees a finished script as one, and nothing has to be written there to keep it from doing so. There is nothing to read on one and no way to build one: the call is what raises it.

The examples below show a script that stops once its answer is written, an error report that stays silent when a script merely finished, and a request answered from deep inside a helper with the access log still written.

Examples

A script that stops once its answer is written

<?nvs
// `Core\Script::finish()` stops a script that has written all its output.
// The code after the call does not run. A `finally` block still runs,
// so a file that is open is still closed.
class Export
{
public static function run(array<string> $rows): void
{
echo "opening the export file\n";
try {
if (Core\Arr::count($rows) == 0) {
echo "nothing to export\n";
Core\Script::finish();
}
foreach ($rows as string $row) {
echo 'writing ', $row, "\n";
}
} finally {
// Runs whether the export finished its rows or stopped early.
echo "closing the export file\n";
}
}
}
array<string> $rows = [];
Export::run($rows);
echo "this line is never reached\n";
Output
opening the export file
nothing to export
closing the export file

Finishing is not failing

<?nvs
// Work wrapped in a handler that reports anything going wrong. A script that
// merely finished is not one of those things: no `catch` takes it, so the
// report stays quiet while the cleanup beside it still runs.
class Job
{
public static function run(string $name): void
{
if ($name == 'broken') {
throw new RuntimeError('the job gave up');
}
echo "the job did its work\n";
Core\Script::finish();
}
}
class Guard
{
public static function around(string $name): void
{
try {
Job::run($name);
} catch (Throwable $failure) {
// Only a real failure gets here. The end of a finished script
// passes straight through this arm.
echo 'reporting: ', $failure->message, "\n";
} finally {
echo 'handler done with ', $name, "\n";
}
}
}
Guard::around('broken');
Guard::around('nightly');
echo "this line is never reached\n";
Output
reporting: the job gave up
handler done with broken
the job did its work
handler done with nightly

An answer from deep inside a request

<?nvs
// This program stops a request from a function deep inside it.
// The browser already has this version of the page, so the page is not built.
// `Core\Script::finish()` skips the rest of the work.
// The outer `finally` block still runs and writes the log line.
class Cache
{
public static function isFresh(string $sent, string $current): bool
{
return $sent == $current;
}
}
class Page
{
public static function render(string $sentVersion): void
{
echo "looking up the article\n";
if (Cache::isFresh($sentVersion, 'v7')) {
echo "304 not modified\n";
Core\Script::finish();
}
echo "building the page\n";
}
}
class Request
{
public static function handle(string $sentVersion): void
{
try {
Page::render($sentVersion);
echo "200 ok\n";
} finally {
echo "access log: request served\n";
}
}
}
Request::handle('v7');
echo "this line is never reached\n";
Output
looking up the article
304 not modified
access log: request served