|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-05-04 21:29 UTC] mike at mikegerwitz dot com
Description:
------------
PDOException extends RuntimeException which in turn extends Exception. The
Exception class has the following constructor:
public __construct ([ string $message = "" [, int $code = 0 [, Exception
$previous = NULL ]]] )
The $code parameter is to be of type integer. However, when using pdo_odbc or
pdo_dblib, the error code returned by the server may be a string, rather than an
integer. So, for example, calling $e->getCode() may yield a value of "12X34".
This is inconsistent with the method definition as well:
final public int getCode ( void )
PHP's own internal library should not produce outcomes that are in conflict with
PHP's definitions.
Test script:
---------------
try
{
$pdo->query( 'BAD QUERY' );
}
catch ( PDOException $e )
{
throw new Exception( $e->getMessage(), $e->getCode() );
}
Expected result:
----------------
// Just a thrown exception, nothing important
Actual result:
--------------
PHP Notice: A non well formed numeric value encountered in [...] on line 7
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 09:00:01 2025 UTC |
Here is some working example code, Excpetion::getCode() is final, however Exception::$code protected. /** * Class PDOStatementException */ class PDOStatementException extends PDOException { /** * @var string */ private $statement; /** * @var string */ protected $code; public static function createFrom(PDOException $PDOException, $statement) { if (!is_string($statement)) { throw new InvalidArgumentException( sprintf('Statement must be stirng, %s given', gettype($statement)) ); } $message = $PDOException->getMessage() . " in statement '" . $statement . "'"; $sqlstate = $PDOException->getCode(); if (!is_string($sqlstate)) { throw new LogicException( sprintf('Expected type of PDOException::getCode() to be string, %s given', gettype($sqlstate)) ); } $exception = new PDOStatementException($message, 0, $PDOException); $exception->code = $sqlstate; $exception->statement = $statement; return $exception; } /** * @return string */ public function getStatement() { return $this->statement; } }