2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-01-27 13:42:22 +01:00
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.
2020-12-21 15:38:52 +01:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
Having a subclass and a parent class in the same ``++catch++`` clause is also useless. It is enough to keep only the parent class.
2020-12-21 15:38:52 +01:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
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.
2020-12-21 15:38:52 +01:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-12-21 15:38:52 +01:00
2022-02-04 17:28:24 +01:00
[source,php]
2020-12-21 15:38:52 +01:00
----
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();
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-12-21 15:38:52 +01:00
2022-02-04 17:28:24 +01:00
[source,php]
2020-12-21 15:38:52 +01:00
----
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();
}
----
2023-05-03 11:06:20 +02:00
== Resources
2020-12-21 15:38:52 +01:00
* RFC - https://wiki.php.net/rfc/multiple-catch[Catching Multiple Exception Types]
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
endif::env-github,rspecator-view[]