note 118373 added to pdo.construct

From: Date: Tue, 24 Nov 2015 19:03:34 +0000
Subject: note 118373 added to pdo.construct
Groups: php.notes 
Request: Send a blank email to php-notes+get-204220@lists.php.net to get a copy of this message
<?php // PDO::__construct can end up with a fatal error rather than throw an exception even if you try ... catch ... . try{ $db = new PDO( // a database to which connection may fail 'mysql:host=www.wuxiancheng.cn; dbname=php; port=3306; charset=utf8', 'root', 'rx00t', array( PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION, PDO::ATTR_TIMEOUT=>1, ) ); }catch(PDOException $pe){ // test the following lines of code respectively, and you will find the first line and the second line will end up with a fatal error. throw $pe; // Fatal error: in path\file.php on line n, script ends, no exception thrown throw new PDOException($pe->getMessage()); //Fatal error: in path\file.php on line n, script ends, no exception thrown throw new PDOException ('cannot connect to the database'); // an exception is thrown as expected throw new PDOException('error: '.$pe->getMessage()); //Fatal error: in path\file.php on line n, script ends, no exception thrown throw new PDOException($pe->getLine()); // an exception is thrown as expected throw new PDOException($pe->getFile()); // an exception is thrown as expected throw new PDOException($pe->getCode()); // an exception is thrown as expected throw new PDOException($pe->getTraceAsString()); // an exception is thrown as expected } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 118.117.77.70 ---- Manual Page -- http://php.net/manual/en/pdo.construct.php Edit -- https://master.php.net/note/edit/118373 Del: integrated -- https://master.php.net/note/delete/118373/integrated Del: useless -- https://master.php.net/note/delete/118373/useless Del: bad code -- https://master.php.net/note/delete/118373/bad+code Del: spam -- https://master.php.net/note/delete/118373/spam Del: non-english -- https://master.php.net/note/delete/118373/non-english Del: in docs -- https://master.php.net/note/delete/118373/in+docs Del: other reasons-- https://master.php.net/note/delete/118373 Reject -- https://master.php.net/note/reject/118373 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#204220) next »