Skip to content

IOError

The error a program throws when something outside it failed: a file is missing, a disk is full, or a network connection closed too early.

IOError is a kind of RuntimeError. A RuntimeError reports a problem that comes from outside the program. A LogicError reports a bug in the program itself. This matters when you catch the error. A bug is fixed in the code. An IOError is handled where the call is made, because the same call can succeed later or on another machine. You can use a fallback value, report the problem and continue, or try again.

Every error has a message and the place it was thrown from. An error can also have previous, which is the earlier error that caused it. With both, a report can show what the program was doing and what went wrong.

The examples below show a function that throws an IOError, an IOError that keeps the first error in previous, and a page that tries three sources in order until one works.

Examples

An operation that could not be done

<?nvs
// An IOError says that something outside the program did not happen, and names
// what was being attempted. Code that raises one is usually a small helper
// standing in front of a machine, a disk or a network.
class Store
{
public static function fetch(string $name): string
{
// Stands in for the operation: nothing here is the program's mistake,
// so the answer is a refusal the caller can act on.
if ($name != 'settings') {
throw new IOError('no document called ' . $name);
}
return 'theme=dark';
}
}
echo Store::fetch('settings'), "\n";
try {
echo Store::fetch('holidays'), "\n";
} catch (IOError $missing) {
echo 'could not load it: ', $missing->message, "\n";
echo "carrying on with the defaults\n";
}
Output
theme=dark
could not load it: no document called holidays
carrying on with the defaults

The failure underneath

<?nvs
// A helper usually knows what it was doing, and the failure underneath knows
// what went wrong. Keeping both means the report can say the first and still
// show the second.
class Report
{
public static function load(string $document): string
{
try {
Core\Json::decode($document);
return 'loaded';
} catch (ParseError $cause) {
// The cause travels with the new error instead of being replaced
// by it.
throw new IOError('the saved report is unusable', { previous: $cause });
}
}
}
try {
echo Report::load('{oops}'), "\n";
} catch (IOError $failed) {
echo $failed->message, "\n";
// `previous` is empty for an error raised on its own, so it is read through
// a binding the compiler can see is no longer null.
var $cause = $failed->previous;
if ($cause != null) {
echo ' because: ', $cause->message, "\n";
}
}
Output
the saved report is unusable
because: Core\Json::decode(): key must be a string at line 1 column 2

A page that stays up

<?nvs
// What this looks like at work: a value can come from more than one place, and
// each place may be unreachable. Trying them in order means one machine being
// down shows up as a slower page rather than a broken one.
class Prices
{
public static function from(string $source): string
{
if ($source == 'cache') {
throw new IOError('the cache server is not answering');
}
if ($source == 'database') {
throw new IOError('the database is not accepting connections');
}
return "yesterday's prices";
}
}
array<string> $places = ['cache', 'database', 'file'];
string $prices = '';
foreach ($places as string $place) {
try {
$prices = Prices::from($place);
echo 'served from ', $place, "\n";
break;
} catch (IOError $unreachable) {
// Worth saying out loud: the page is fine, and somebody should still
// know this happened.
echo 'skipping ', $place, ': ', $unreachable->message, "\n";
}
}
echo $prices, "\n";
Output
skipping cache: the cache server is not answering
skipping database: the database is not accepting connections
served from file
yesterday's prices