以下の内容はhttps://r9.hateblo.jp/entry/2015/12/08/233520より取得しました。


PHP7のThrowableを利用したキャッチ

PHP7からErrorクラスが新設されて、文法エラーのような例外も捕捉できるようになった。

Errorクラスも既存のExceptionクラスも、Throwableインターフェースを実装しているので、完全なキャッチは以下のようになる、と思う。

<?php

function add(int $a, int $b): int {
    return $a + $b;
}

echo add(123, 234), "\n"; // => 357

//try {
//    echo add('hoge', 'fuga'), "\n";
//}
//catch (Exception $e) {
//    echo "Exception: ", $e->getMessage(), "\n";
//}
//
// 拾えずに「PHP Fatal error:  Uncaught TypeError: Argument 1 passed to add() must be of the type integer, string given〜」が発生する

try {
    echo add('hoge', 'fuga'), "\n";
}
catch (\Exception $e) {
    echo "Exception: ", $e->getMessage(), "\n";
}
catch (\Throwable $e) {
    echo "Throwable: ", $e->getMessage(), "\n";
}

// 「Throwable: Argument 1 passed to add() must be of the type integer, string given〜」で拾える

echo "finish\n"; // この処理も継続して動く



以上の内容はhttps://r9.hateblo.jp/entry/2015/12/08/233520より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14