Part 3 · Flow & errors / Checked errors
Lesson 7 of 8

Checked errors

Declare what a function can throw with a throws clause. A checked error that can escape a callable must be caught or declared, verified across the whole project:

users.ppphp
function loadUser(string $id): string
    throws UserNotFound, StorageFailure

The caller in the editor handles UserNotFound but forgets StorageFailure. Press Check to see the compiler catch it, then add the missing catch block after the first one:

users.ppphp
} catch (StorageFailure $error) {
    echo 'unavailable';
}

Check again and the file passes. At runtime these remain ordinary PHP exceptions; the clause is erased into PHPDoc metadata.