Skip to content

Instantly share code, notes, and snippets.

@ThijsFeryn
Created November 30, 2015 14:17
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/70ca08ad9cbbd65f622c to your computer and use it in GitHub Desktop.
Save ThijsFeryn/70ca08ad9cbbd65f622c to your computer and use it in GitHub Desktop.
PHP 7 throwables
<?php
/**
* Throwable interface
*/
//Error as Throwable
try {
sqdf();
} catch (Throwable $t) {
echo "Throwable: ".$t->getMessage().PHP_EOL;
}
//Exception as Throwable
try {
throw new Exception("Bla");
} catch (Throwable $t) {
echo "Throwable: ".$t->getMessage().PHP_EOL;
}
//Error
try {
sqdf();
} catch (Error $e) {
echo "Error: ".$e->getMessage().PHP_EOL;
} catch (Exception $e) {
echo "Exception: ".$e->getMessage().PHP_EOL;
}
//Exception
try {
throw new Exception("Bla");
} catch (Error $e) {
echo "Error: ".$e->getMessage().PHP_EOL;
} catch (Exception $e) {
echo "Exception: ".$e->getMessage().PHP_EOL;
}
//Type error
try {
function add(int $a, int $b):int {
return $a + $b;
}
echo add(array(), array());
} catch (TypeError $t) {
echo "Type error: ".$t->getMessage().PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment