|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-11-24 11:33 UTC] marcos dot neves at gmail dot com
Description:
------------
The follow code is ok, cause 123, even as a string, it can be convert to number.
new Exception("Message", "123");
The follow is not ok.
new Exception("Message", "NotNumber");
PHP is not a typed language, why Exception must have a number as code?
The problem is that error code doesn?t means "Number error code" but could be a string error code, like mysql 5.1 uses.
Check the follow reproduce able code.
Reproduce code:
---------------
<?
error_reporting(E_ALL);
$host = "localhost";
$dbName = "test";
$user = "root";
$passwd = "";
$mysqlDsn = "mysql:host=$host;$dbName";
$pdo = new PDO($mysqlDsn, $user, $passwd);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$pdo->query("SELECT version");
}catch(Exception $e){
$code = $e->getCode(); // 42S22
echo "<pre>";
var_dump($code); // string(5) "42S22"
throw new Exception($e->getMessage(), $code);
}
?>
Expected result:
----------------
The follow line, should accept the $code var, but doesn?t.
throw new Exception($e->getMessage(), $code);
It?s not consistent, since the Exception object constructor can?t accept a non number as code, but it can return.
See:
$msg = $catchedPdoMysqlException->getMessage();
$code = $catchedPdoMysqlException->getCode();
new Exception($msg, $code); // $code is not valid one.
Actual result:
--------------
Exception code, should accept non numbers as a code value, as mysql does.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 14 14:00:01 2025 UTC |
Ubuntu 10.04.1 LTS \n \l PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:01:00) MySQL Server version: 5.1.41-3ubuntu12.5 (Ubuntu) When executing a statement which generates: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '{REDACTED}' for key 'PRIMARY' The following test does not pass when the exception is caught: if ($exception->getCode() === 23000) However, the following code DOES pass when the exception is caught: if ((int)$exception->getCode() === 23000) The API documentation for getCode() states that it returns an integer and that is clearly not the case in this example. I merely request that the function and the documentation be consistent.