Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

ฟังก์ชัน set_exception_handler() ใน PHP

คำอธิบาย

ฟังก์ชัน set_exception_handler() กำหนดฟังก์ชันผู้ใช้เพื่อจัดการข้อยกเว้น (exception) ที่ไม่ถูกจับใน try/catch block การทำงานสคริปต์จะหยุดหลังจากเรียก exception handler

รูปแบบการใช้งาน (Syntax)

set_exception_handler(exception_handler)

พารามิเตอร์

  • exception_handler — ชื่อฟังก์ชันที่จะเรียกเมื่อมีข้อยกเว้นไม่ถูกจับ ฟังก์ชันต้องรับพารามิเตอร์ 1 ตัวคือออบเจ็กต์ Exception

ค่าที่คืนกลับ

คืนค่าชื่อ exception handler ก่อนหน้า หรือ NULL หากไม่มีหรือเกิดข้อผิดพลาด

ตัวอย่างการใช้งาน

<?php
function exception_handler($exception) {
    echo "Uncaught exception: " . $exception->getMessage() . "\n";
}

set_exception_handler('exception_handler');
throw new Exception('Not Found Exception');
echo "This line will not execute\n";
?>

ผลลัพธ์

Uncaught exception: Not Found Exception