Exception Extension
ID |
php.exception_extension |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Php |
Tags |
exception-handling, reliability |
Description
Reports a class whose name ends in Exception but which does not extend anything. The
Exception suffix advertises an exception type, yet a class that extends nothing is not a
Throwable: it cannot be thrown and cannot be caught with catch (Exception $e).
Rationale
PHP only allows objects implementing Throwable to be thrown, and user code conventionally
defines custom exceptions by extending \Exception or a more specific subclass. A class named
PaymentException that extends nothing looks like an exception to every reader but behaves
like a plain data object, so throw new PaymentException(…) is a fatal error and
catch (Exception) never matches it. Requiring an extends clause keeps the name and the
behaviour aligned.
<?php
class PaymentException // FLAW — named like an exception but extends nothing
{
}
class ValidationException extends Exception // OK — extends a base exception
{
}
class NotFoundException extends RuntimeException // OK — extends another exception
{
}