1.2.1043. Rethrown Exceptions

Throwing a caught exception is usually useless and dead code.

When exceptions are caught, they should be processed or transformed, but not rethrown as is.

Those issues often happen when a catch structure was positioned for debug purposes, but lost its usage later.

<?php

try {
    doSomething();
} catch (Exception $e) {
    throw $e;
}

?>

See also What are the best practices for catching and re-throwing exceptions?.

1.2.1043.1. Suggestions

  • Log the message of the exception for later usage.

  • Remove the try/catch and let the rest of the application handle this exception.

  • Chain the exception, by throwing a new exception, including the caught exception.

1.2.1043.2. Specs

Short name

Exceptions/Rethrown

Rulesets

All, Changed Behavior, Dead code

Exakat since

0.9.0

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

exception

Examples

PrestaShop

Available in

Entreprise Edition, Exakat Cloud