Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThijsFeryn/7611b79e9812d89fe9e017421f2a8b67 to your computer and use it in GitHub Desktop.
Save ThijsFeryn/7611b79e9812d89fe9e017421f2a8b67 to your computer and use it in GitHub Desktop.
Catch a type error either as a type error, an error, or a throwable
<?php
function double(int $argument):int {
return $argument*2;
}
try {
var_dump(double([3]));
} catch (TypeError $e) {
echo "Caught as type error: ".$e->getMessage().PHP_EOL;
}
try {
var_dump(double([3]));
} catch (Error $e) {
echo "Caught as error: ".$e->getMessage().PHP_EOL;
}
try {
var_dump(double([3]));
} catch (Throwable $e) {
echo "Caught as throwable: ".$e->getMessage().PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment