Req #39615 [Opn->Sus]: Exception code can not be string
Edit report at https://bugs.php.net/bug.php?id=39615&edit=1
ID: 39615
Updated by: cmb@php.net
Reported by: marcos dot neves at gmail dot com
Summary: Exception code can not be string
-Status: Open
+Status: Suspended
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: *
PHP Version: *
Block user comment: N
Private report: N
New Comment:
It makes no sense to leave this unresolved for another five years,
so for the time being, I'm suspending this ticket. If anybody is
still interested in this feature, please pursue the RFC
process[1].
Also note that it is already possible to store *arbitrary* values
in the ::$code property of own exception types[1].
[1] <https://wiki.php.net/rfc/howto>
[2] <https://3v4l.org/KdCAm>
Previous Comments:
------------------------------------------------------------------------
[2015-06-06 16:08:03] cmb@php.net
> Allowing a non-numeric $code in the Exception constructor seems
> like the right solution.
That might constitute a considerable BC break, and would probably
require an RFC.
------------------------------------------------------------------------
[2015-06-06 14:49:02] cmb@php.net
Related To: Bug #51742
------------------------------------------------------------------------
[2011-04-25 18:41:00] bandy dot chris at gmail dot com
Related to bug 51742.
Allowing a non-numeric $code in the Exception constructor seems like the right solution.
------------------------------------------------------------------------
[2010-08-04 05:22:49] bryan at ravensight dot org
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.
------------------------------------------------------------------------
[2006-11-24 11:33:53] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=39615&edit=1
Thread (6 messages)