LoginSignup
1
2

More than 5 years have passed since last update.

PHPの例外って

Posted at

PHPの例外、特にfinallyが思った通り1に動作する。
すてき。

環境情報
$ php -v
PHP 5.6.29 (cli) (built: Dec  9 2016 07:03:56)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
さんぷるこーど
<?php
error_reporting( -1 ) ;

echo call_user_func( function ($human) {
    try{
        throw new Exception('( ゚∀゚)') ; // (1)
    }
    catch ( \Exception $e ) {
        echo $e->getMessage() ; // (2)
        return '(゚∀゚ )' ; // (4)
    }
    finally {
        echo $human ; // (3)
    }
}, '人') . PHP_EOL ;

//=> ( ゚∀゚)人(゚∀゚ )

参考と注釈


  1. PHP 5.5 以降では、catch ブロックの後に finally ブロックも指定できるようになりました。 finally ブロックの何かに書いたコードは、 try および catch ブロックの後で常に実行されます。例外がスローさされたかどうかには関係ありません。 

1
2
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
1
2