Errors with a default in one line
Often you want a default value when a call fails. In Novis you write catch after the expression:
<?nvs// This program reads a setting and uses a default when the setting is missing.class Settings{ public static function get(string $key): int { if ($key != 'port') { throw new IOError("no setting named $key"); } return 8080; }}
// `catch` after an expression gives the value on the right when the expression throws.echo Settings::get('port') catch (IOError $e) => 80, "\n";echo Settings::get('workers') catch (IOError $e) => 4, "\n";
// A `try` block works too. The message says what went wrong.try { Settings::get('timeout');} catch (IOError $e) { echo $e->message, "\n";}80804no setting named timeoutSettings::get("workers") throws an IOError, so the result is 4. A try block works the same way
when you need more than one line.
Throwing an error is fast in Novis. You can throw errors for missing records and failed checks. This is true on a busy web server too.
Read more in Syntax: the catch expression and Syntax: throw and catch.
