コンストラクタやデストラクタの中で例外をスローコンストラクタで例外をスロー <?php
class Foo {
function __construct() {
throw new Exception('Error: __construct()');
}
}
try {
$f = new Foo;
print "OK\n";
} catch (Exception $e) {
print $e->getMessage() . "\n";
}
Error: __construct() OK。 デストラクタで例外をスロー <?php
class Bar {
function __construct() { }
function __destruct() {
throw new Exception('Error: __destruct()');
}
}
try {
$b = new Bar;
print "OK\n";
} catch (Exception $e) {
print $e->getMessage() . "\n";
}
OK
PHP Fatal error: Uncaught exception 'Exception' with message 'Error: __destruct()' in /home/taro/tmp/1.php:18
Stack trace:
#0 [internal function]: Bar->__destruct()
#1 {main}
thrown in /home/taro/tmp/1.php on line 18
ダメ。 try {
$b = new Bar;
unset($b);
print "OK\n";
} catch (Exception $e) {
print $e->getMessage() . "\n";
}
Error: __destruct() OK。 |
|