1.2.1378. Useless Catch

A catch clause should handle the exception by doing something.

Among the task of a catch clause : log the exception, clean any mess that was introduced, fail graciously.

In particular, a return inside a catch clause might short-circuit the commands laid after the try/catch block.

It is also a sign that there is no error, and the exception shall be handled with a preemptive check, rather than an error review.

<?php

function foo($a) {
    try {
        $b = doSomething($a);
    } catch (Throwable $e) {
        // No log of the exception : no one knows it happened.

        // return immediately ?
        return false;
    }

    $b->complete();

    return $b;
}

?>

See also Exceptions and Best practices for PHP exception handling.

1.2.1378.1. Suggestions

  • Add a log call to the catch block.

  • Handle correctly the exception.

1.2.1378.2. Specs

Short name

Exceptions/UselessCatch

Rulesets

All

Exakat since

1.1.4

PHP Version

All

Severity

Minor

Time To Fix

Slow (1 hour)

Precision

High

Features

exception, catch

Examples

Zurmo, PrestaShop

Available in

Entreprise Edition, Community Edition, Exakat Cloud