Current situation from the manual:
-
Interface method Throwable::getCode() promises to return int.
-
The Error class and the Exception class both implement Throwable,
but their constructor accepts $code as int while their getCode() method
returns mixed.
The reason, reveals the Exception::getCode() manual page, is that the
getCode() method "Returns the exception code as integer in Exception but
possibly as other type in Exception descendants (for example as string
in PDOException).". Then, basically, getCode() may return anything,
then it should be declared to return "mixed".
Maybe is not too late to fix this mess, which is confusing for the user,
impossible to validate, and basically useless (nobody knew what these
int codes stand for before, now the situation is even worse).
So here is my proposal:
-
Remove the getCode() method from Throwable and Error. The "error code"
concept still remains in the Exception class for backward compatibility. -
Specialized classes must define their specialized exceptions with their
specialized feedback informations (error codes or whatever and respective
properties and/or methods); user code that want to take advantage of the
extra infos, may catch these specialized exceptions and it knows how to
handle these data. -
PDOException::getCode() returns 0 as any other class
extending Exception I'm aware of, and instead it may define a
PDOException::getSomethingElseFeedback() returning something useful, (and
if it is only a string, the getMessage() method already fits the need).
Regards,
/|\ Umberto Salsi
/_/ www.icosaedro.it