ฟังก์ชั่น set_exception_handler()
ความหมายของฟังก์ชั่นและการใช้งาน
การตั้งค่าฟังก์ชัน ตัวจัดการข้อยกเว้นที่กำหนดโดยผู้ใช้ ด้วยฟังก์ชั่น set_exception_handler()
รูปแบบการเขียน (Syntax)
callable set_exception_handler ( callable $exception_handler )
ตัวอย่างที่ 1
<?php
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\n";
}
set_exception_handler('exception_handler');
throw new Exception('Uncaught Exception');
echo "Not Executed\n";
?>
ตัวอย่างที่ 2
<?php
class example {
public function __construct() {
@set_exception_handler(array($this, 'exception_handler'));
throw new Exception('DOH!!');
}
public function exception_handler($exception) {
print "Exception Caught: ". $exception->getMessage() ."\n";
}
}
$example = new example;
?>