Skip to content

Latest commit

 

History

History
38 lines (31 loc) · 705 Bytes

MultipleCatch.md

File metadata and controls

38 lines (31 loc) · 705 Bytes

MultipleCatch

In PHP 7.1 and later, you can specify multiple exceptions in a catch block using the pipe (|) character. This is useful for when different exceptions from different class hierarchies are handled the same.

Before

<?php
try {
    hoge();
} catch (A $exn) {
    echo "catch!";
    fuga();
} catch (B $exn) { // MultipleCatch: Specifying `B` and `C` in this catch block avoids redundant catch blocks.
    echo "catch!";
} catch (C $exn) {
    echo "catch!";
}

After

<?php
try {
    hoge();
} catch (A $exn) {
    echo "catch!";
    fuga();
} catch (B | C $exn) { // OK!
    echo "catch!";
}

Reference

https://secure.php.net/manual/language.exceptions.php