ข้อยกเว้น PHP
ข้อยกเว้นคืออะไร?
ข้อยกเว้นคืออ็อบเจ็กต์ที่อธิบายข้อผิดพลาดหรือลักษณะการทำงานที่ไม่คาดคิดของสคริปต์ PHP
ข้อยกเว้นเกิดขึ้นจากฟังก์ชันและคลาสของ PHP จำนวนมาก
ฟังก์ชั่นและคลาสที่ผู้ใช้กำหนดยังสามารถโยนข้อยกเว้น
ข้อยกเว้นเป็นวิธีที่ดีในการหยุดฟังก์ชันเมื่อพบข้อมูลที่ไม่สามารถใช้ได้
โยนข้อยกเว้น
คำthrow
สั่งอนุญาตให้ผู้ใช้กำหนดฟังก์ชันหรือวิธีการส่งข้อยกเว้น เมื่อมีการส่งข้อยกเว้น รหัสที่ตามมาจะไม่ถูกดำเนินการ
หากไม่พบข้อยกเว้น ข้อผิดพลาดร้ายแรงจะเกิดขึ้นพร้อมกับข้อความ "Uncaught Exception"
ให้ลองโยนข้อยกเว้นโดยไม่จับมัน:
ตัวอย่าง
<?php
function divide($dividend, $divisor) {
if($divisor == 0)
{
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
echo divide(5, 0);
?>
ผลลัพธ์จะมีลักษณะดังนี้:
Fatal error: Uncaught Exception: Division by zero in
C:\webfolder\test.php:4
Stack trace: #0 C:\webfolder\test.php(9):
divide(5, 0) #1 {main} thrown in C:\webfolder\test.php on line 4
ลอง...จับงบ
เพื่อหลีกเลี่ยงข้อผิดพลาดจากตัวอย่างข้างต้น เราสามารถใช้
try...catch
คำสั่งเพื่อตรวจจับข้อยกเว้นและดำเนินการตามขั้นตอนต่อไป
ไวยากรณ์
try {
code that can throw exceptions
} catch(Exception $e) {
code that runs when an exception is caught
}
ตัวอย่าง
แสดงข้อความเมื่อมีข้อยกเว้น:
<?php
function divide($dividend, $divisor) {
if($divisor ==
0) {
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
try {
echo
divide(5, 0);
} catch(Exception $e) {
echo "Unable to divide.";
}
?>
บล็อก catch ระบุว่าควรตรวจจับข้อยกเว้นประเภทใดและชื่อของตัวแปรที่สามารถใช้เพื่อเข้าถึงข้อยกเว้น ในตัวอย่างข้างต้น ประเภทของข้อยกเว้นคือException
และชื่อตัวแปร$e
คือ
ความพยายาม...จับ...ในที่สุดงบ
คำtry...catch...finally
สั่งสามารถใช้เพื่อตรวจจับข้อยกเว้น รหัสใน
finally
บล็อกจะทำงานเสมอไม่ว่าจะตรวจพบข้อยกเว้นหรือไม่ หาก
finally
มีcatch
บล็อกเป็นตัวเลือก
ไวยากรณ์
try {
code that can throw exceptions
} catch(Exception $e) {
code that runs when an exception is caught
} finally {
code that
always runs regardless of whether an exception was caught
}
ตัวอย่าง
แสดงข้อความเมื่อมีข้อยกเว้น แล้วระบุว่ากระบวนการสิ้นสุดแล้ว:
<?php
function divide($dividend, $divisor) {
if($divisor
== 0) {
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
try {
echo
divide(5, 0);
} catch(Exception $e) {
echo "Unable to
divide. ";
} finally {
echo "Process complete.";
}
?>
ตัวอย่าง
ส่งออกสตริงแม้ว่าจะไม่พบข้อยกเว้น:
<?php
function divide($dividend, $divisor) {
if($divisor == 0)
{
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
try {
echo divide(5, 0);
} finally {
echo
"Process complete.";
}
?>
วัตถุข้อยกเว้น
วัตถุข้อยกเว้นมีข้อมูลเกี่ยวกับข้อผิดพลาดหรือลักษณะการทำงานที่ไม่คาดคิดที่ฟังก์ชันพบ
ไวยากรณ์
new Exception(message, code, previous)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
message | Optional. A string describing why the exception was thrown |
code | Optional. An integer that can be used used to easily distinguish this exception from others of the same type |
previous | Optional. If this exception was thrown in a catch block of another exception, it is recommended to pass that exception into this parameter |
วิธีการ
เมื่อตรวจพบข้อยกเว้น ตารางต่อไปนี้จะแสดงวิธีการบางอย่างที่สามารถใช้เพื่อรับข้อมูลเกี่ยวกับข้อยกเว้น:
Method | Description |
---|---|
getMessage() | Returns a string describing why the exception was thrown |
getPrevious() | If this exception was triggered by another one, this method returns the previous exception. If not, then it returns null |
getCode() | Returns the exception code |
getFile() | Returns the full path of the file in which the exception was thrown |
getLine() | Returns the line number of the line of code which threw the exception |
ตัวอย่าง
ส่งออกข้อมูลเกี่ยวกับข้อยกเว้นที่ถูกส่งออกไป:
<?php
function divide($dividend, $divisor) {
if($divisor == 0)
{
throw new Exception("Division by zero", 1);
}
return $dividend / $divisor;
}
try {
echo
divide(5, 0);
} catch(Exception $ex) {
$code = $ex->getCode();
$message = $ex->getMessage();
$file = $ex->getFile();
$line = $ex->getLine();
echo "Exception thrown in $file on line
$line: [Code $code]
$message";
}
?>
อ้างอิงข้อยกเว้นที่สมบูรณ์
สำหรับข้อมูลอ้างอิงที่สมบูรณ์ ไปที่Complete PHP Exception Referenceของเรา
ข้อมูลอ้างอิงประกอบด้วยคำอธิบายและตัวอย่างของวิธีการยกเว้นทั้งหมด