44 lines
1.2 KiB
Plaintext
44 lines
1.2 KiB
Plaintext
Repeating an exception class in a single ``catch`` clause will not fail but it is not what the developer intended. Either the class is not the one which should be caught, or this is dead code.
|
||
|
||
Having a subclass and a parent class in the same ``catch`` clause is also useless. It is enough to keep only the parent class.
|
||
|
||
This rule raises an issue when an exception class is duplicated in a ``catch`` clause, or when an exception class has a parent class in the same ``catch`` clause.
|
||
|
||
== Noncompliant Code Example
|
||
|
||
----
|
||
try {
|
||
throw new CustomException();
|
||
} catch(CustomException | Exception $e) { // Noncompliant. CustomException inherits from Exception
|
||
echo $e->message();
|
||
}
|
||
|
||
try {
|
||
throw new CustomException();
|
||
} catch(Exception | Exception $e) { // Noncompliant.
|
||
echo $e->message();
|
||
}
|
||
----
|
||
|
||
== Compliant Solution
|
||
|
||
----
|
||
try {
|
||
throw new CustomException();
|
||
} catch(Exception $e) {
|
||
echo $e->message();
|
||
}
|
||
|
||
try {
|
||
throw new CustomException();
|
||
} catch(CustomException $e) {
|
||
echo $e->getCustomMessage();
|
||
} catch(Exception $e) {
|
||
echo $e->message();
|
||
}
|
||
----
|
||
|
||
== See
|
||
|
||
* RFC - https://wiki.php.net/rfc/multiple-catch[Catching Multiple Exception Types]
|