LoginSignup
21
20

More than 5 years have passed since last update.

PHP7.0.0α2で例外の扱いが変わった

Last updated at Posted at 2015-06-26

PHP7.0.0α1ではパースエラーをParseExceptionで受け取れます。
http://qiita.com/hnw/items/4e2d47d269a26025a726
http://qiita.com/rana_kualu/items/447995347acb2f8997a3
が、PHP7.0.0α2に上げたら何故かできなくなってしまいました。
調べてみたら構造が変わってました。

Throwable ( interface )
    Exception
        LogicException
            InvalidArgumentExceptionとか
        RuntimeException
            OutOfBoundsExceptionとか
        ErrorException
        ReflectionExceptionとか
    Error
        ParseError
        TypeError
        AssertionError

各ExceptionとErrorはインターフェイスThrowableをimplementsするようになっていました。
BaseExceptionとEngineExceptionは早くも消滅。
結果としてExceptionとErrorは基本別物となりました。
ところでErrorExceptionとはいったい。

    // ParseError
    try{
        require_once('fail.php');
    }catch(ParseError $e){
        var_dump($e->getMessage()); // syntax error, unexpected end of file
    }

    // TypeError
    try{
        new PDO();
    }catch(TypeError $e){
        var_dump($e->getMessage()); // PDO::__construct() expects at least 1 parameter, 0 given
    }

    // AssertionError
    try{
        assert_options(ASSERT_EXCEPTION, 1); // 例外を吐くようにする。デフォルトはE_WARNING
        assert(false);
    }catch(AssertionError $e){
        var_dump($e->getMessage()); // assert(false)
    }

    // 使い分け
    try{
        assert(false);
    }catch(Exception $e){
        // Exceptionではキャッチできない
    }catch(Error $e){
        // ここに来る
    }
21
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
20