1.2.431. Exception Order

When catching exception, the most specialized exceptions must be in the early catch, and the most general exceptions must be in the later catch. Otherwise, the general catches intercept the exception, and the more specialized will not be read.

<?php

class A extends \Exception {}
class B extends A {}

try {
    throw new A();
}
catch(A $a1) { }
catch(B $b2 ) {
    // Never reached, as previous Catch is catching the early worm
}

?>

1.2.431.1. Suggestions

  • Remove one of the catch clause

1.2.431.2. Specs

Short name

Exceptions/AlreadyCaught

Rulesets

All, Changed Behavior, Dead code

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

exception

Examples

Woocommerce

Available in

Entreprise Edition, Exakat Cloud