1.2.1098. Should Chain Exception

Chain exception to provide more context.

When catching an exception and rethrowing another one, it is recommended to chain the exception : this means providing the original exception, so that the final recipient has a chance to track the origin of the problem. This doesn’t change the thrown message, but provides more information.

Note : Chaining requires PHP > 5.3.0.

<?php
    try {
        throw new Exception('Exception 1', 1);
    } catch (\Exception $e) {
        throw new Exception('Exception 2', 2, $e);
        // Chaining here.

    }
?>

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

1.2.1098.1. Suggestions

  • Add the incoming exception to the newly thrown exception

1.2.1098.2. Specs

Short name

Structures/ShouldChainException

Rulesets

All, Analyze, CE, CI-checks

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Instant (5 mins)

Precision

Very high

Features

exception-chain

Examples

Magento, Tine20

Available in

Entreprise Edition, Community Edition, Exakat Cloud