1.2.422. Empty Try Catch¶
The code does try, then catch errors but do no act upon the error.
At worst, the error should be logged, so as to measure the actual usage of the catch expression.
catch( `Exception <https://www.php.net/exception>`_ $e)
(PHP 5) or catch(`Throwable <https://www.php.net/manual/en/class.`throwable <https://www.php.net/throwable>`_.php>`_ $e)
with empty catch block should be banned. They ignore any error and proceed as if nothing happened. At worst, the event should be logged for future analysis.
<?php
try {
doSomething();
} catch (Throwable $e) {
// ignore this
}
?>
See also Empty Catch Clause.
1.2.422.1. Connex PHP features¶
1.2.422.1.1. Suggestions¶
Add some logging in the catch
Add a comment to mention why the catch is empty
Change the exception, chain it and throw again
1.2.422.1.2. Specs¶
Short name |
Structures/EmptyTryCatch |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Examples |
|
Available in |