1.2.1287. Unused Exception Variable

The variable from a catch clause is not used. It is expected to be used, either by chaining the exception, or logging the message.

In PHP 8.0, this variable may be omitted.

<?php

try{
    doSomething();
} catch (A $a) {
    // $a is caught, but not used here
} catch (B $b) {
    // $b is caught, and used
    log($b->getMessage());
} catch (C) {
    // Caught and ignored (PHP 8.0 +)
}

?>

1.2.1287.1. Suggestions

  • Drop the variable in the clause expression (PHP 8.0 and more recent)

  • Chain the exception

  • Log the exception message

1.2.1287.2. Specs

Short name

Exceptions/UnusedExceptionVariable

Rulesets

All, Changed Behavior, Suggestions

Exakat since

2.2.0

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

exception

Available in

Entreprise Edition, Exakat Cloud