Created
November 30, 2015 14:17
-
-
Save ThijsFeryn/70ca08ad9cbbd65f622c to your computer and use it in GitHub Desktop.
PHP 7 throwables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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