CRITICAL
Rule Definition
An empty catch block defeats the purpose of exceptions.
When an exception occurs, nothing happens and the program fails for an unknown reason. The application can be in an unknown state that will affect subsequent processing.
Since the reason for the issue (the type of the exception and potential embedded message) are not known, it will require more time to fix the issue.
Remediation
The exception must be handled properly according to its type.
Violation Code Sample
class CAST{
public function close() {
if (true) {
echo 'test';
}
}
}
Fixed Code Sample
<?php
/**
* Doc comment
* @param int $counter counter
* @throws RuntimeException if counter is 1
*/
function afunction($counter) {
if (1 === $counter) {
throw new RuntimeException("Bad parameter!");
}
}
class ThrowsTagUse {
/**
* Doc comment
* @param int $counter counter
* @throws RuntimeException if counter is 1
*/
public function __construct() {
if (1 === $counter) {
throw new RuntimeException("Bad parameter!");
}
}
/**
* Doc comment
* @param int $counter counter
* @throws RuntimeException if counter is 1
*/
public function aMethod() {
if (1 === $counter) {
throw new RuntimeException("Bad parameter!");
}
}
}
?>
Reference
Code Sniffer - http://pear.php.net/package/PHP_CodeSniffer/docs/1.5.2/PHP_CodeSniffer/Squiz_Sniffs_Commenting_FunctionCommentThrowTagSniff.html
Related Technologies
Technical Criterion
Programming Practices - Error and Exception Handling
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.