Pattern: Empty catch
comment
Issue: -
Checks for empty catch
clause without a comment. It's rarely correct to do nothing in response to a caught exception. Typical responses are to handle it, log it, or if it is considered "impossible", rethrow it.
When it truly is appropriate to take no action whatsoever in a catch block, this rule enforces to have a comment to explain the reason this is justified.
Example of incorrect code:
public function saveFile($filename)
{
$file = PhpFile::instance($filename);
try {
$file->lock();
} catch (Exception $ex) {
}
}
Example of correct code:
public function saveFile($filename)
{
$file = PhpFile::instance($filename);
try {
$file->lock();
} catch (Exception $ex) {
// Another process has locked the file; we will handle this in a bit.
}
}