Skip to content

Core\Fatal::onUncaughtThrow

Registers a function that runs when an error reaches the top of your program and no catch handles it.

The function gets the error object itself, so it can read its message and check its class. Use it to print a clear message for the person who ran the program, or to add a detail you know and the error does not. It runs once. After it returns, the program still ends, the normal error report is still written, and the exit status is still 1.

If the function throws an error itself, that error is ignored and the first one is reported.

Good to know: only one function is registered. A second call replaces the first one. For a resource limit such as memory, use Core\Fatal::onLimit instead.

Parameters

  • $handlercallable(Throwable): mixedThe function to run. It gets the uncaught Throwable: the object the program threw, with its class, message and backtrace. The function returns nothing, and it may have no parameter. If it throws, or uses up what the request has left, it stops at once. The error that is reported is still the original uncaught one.

Return value

Returns void. Nothing. The function belongs to this request only, and a second call replaces the first. It is deleted when the request ends, and no other request can see it. Novis still writes its own record of the error. The exit status does not change.

Examples

Print the error message

<?nvs
// Shows how to print the message of an error that nothing caught.
// proof: exit 1
// This function gets the error object. `$e->message` is the text the error was created with.
Core\Fatal::onUncaughtThrow(fn(Throwable $e): void => {
echo 'the program stopped: ', $e->message, "\n";
});
echo "reading settings\n";
// Nothing catches this error, so the function above runs and the program ends.
throw new RuntimeError('the file settings.json is missing');
Output
reading settings
the program stopped: the file settings.json is missing

A message for each kind of error

<?nvs
// Shows how to give each kind of error its own message.
// proof: exit 1
class NotFound extends RuntimeError
{}
// `is` checks the class of the error. `NotFound` gets a short message.
// Every other error prints its own message.
Core\Fatal::onUncaughtThrow(fn(Throwable $e): void => {
if ($e is NotFound) {
echo 'not found: ', $e->message, "\n";
} else {
echo 'unexpected error: ', $e->message, "\n";
}
});
class Users
{
public static function find(int $id): string
{
if ($id != 1) {
throw new NotFound('user ' . $id);
}
return 'ada';
}
}
echo Users::find(1), "\n";
// There is no user 42, so this prints `not found: user 42`.
echo Users::find(42), "\n";
Output
ada
not found: user 42

A short message in production

<?nvs
// Shows how a command line tool can show a short message to its users
// and the full details only to a developer.
// proof: exit 1
Core\Fatal::onUncaughtThrow(fn(Throwable $e): void => {
if (Core\Env::mode() == Core\Env::Mode::Development) {
echo 'error: ', $e->message, "\n";
} else {
echo "Sorry, the invoice could not be created. Please contact support.\n";
}
});
class Invoices
{
public static function create(string $customer, int $cents): string
{
if ($cents <= 0) {
throw new LogicError('an invoice for ' . $customer . ' needs a positive amount');
}
return 'invoice for ' . $customer . ': ' . $cents . ' cents';
}
}
echo Invoices::create('Shop Ltd', 12900), "\n";
// This amount is not positive, so the error ends the program.
// The mode is `Production` here, so the short message is printed.
echo Invoices::create('Shop Ltd', 0), "\n";
Output
invoice for Shop Ltd: 12900 cents
Sorry, the invoice could not be created. Please contact support.