note 118280 added to language.exceptions

From: Date: Sat, 07 Nov 2015 17:40:32 +0000
Subject: note 118280 added to language.exceptions
Groups: php.notes 
Request: Send a blank email to php-notes+get-204106@lists.php.net to get a copy of this message
Type declarations will trigger Uncaught TypeError when a different type is passed. And it cannot be caught with the Exception class. <?php function xc(array $a){ } try{ xc(4); }catch (Exception $e){ // failed to catch it echo $e->getMessage(); } ?> You should use TypeError instead for PHP 7+, <?php function xc(array $a){ } try{ xc(4); }catch (TypeError $e){ echo $e->getMessage(); } ?> In php version prior to 7.0, you should translate Catchable fatal errors to an exception and then catch it. <?php function exceptionErrorHandler($errNumber, $errStr, $errFile, $errLine ) { throw new ErrorException($errStr, 0, $errNumber, $errFile, $errLine); } set_error_handler('exceptionErrorHandler'); function s(array $a){ } try{ s(4); }catch (Exception $e){ echo $e->getMessage(); } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 118.117.77.220 ---- Manual Page -- http://php.net/manual/en/language.exceptions.php Edit -- https://master.php.net/note/edit/118280 Del: integrated -- https://master.php.net/note/delete/118280/integrated Del: useless -- https://master.php.net/note/delete/118280/useless Del: bad code -- https://master.php.net/note/delete/118280/bad+code Del: spam -- https://master.php.net/note/delete/118280/spam Del: non-english -- https://master.php.net/note/delete/118280/non-english Del: in docs -- https://master.php.net/note/delete/118280/in+docs Del: other reasons-- https://master.php.net/note/delete/118280 Reject -- https://master.php.net/note/reject/118280 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#204106) next »