ข้อยกเว้น (Exception) คือเหตุการณ์ที่เกิดขึ้นระหว่างการดำเนินการของโปรแกรมที่รบกวนการไหลปกติของคำสั่งของโปรแกรม โดยทั่วไป เมื่อสคริปต์ Python พบสถานการณ์ที่ไม่สามารถจัดการได้ มันจะ raise ข้อยกเว้น ข้อยกเว้นคืออ็อบเจ็กต์ Python ที่แทนข้อผิดพลาด
เมื่อสคริปต์ Python raise ข้อยกเว้น มันต้องจัดการข้อยกเว้นทันที มิฉะนั้นจะสิ้นสุดและออก
การจัดการข้อยกเว้น (Handling an exception)
หากคุณมีโค้ดที่น่าสงสัยซึ่งอาจ raise ข้อยกเว้น คุณสามารถปกป้องโปรแกรมของคุณได้โดยการวางโค้ดที่น่าสงสัยในบล็อก try หลังบล็อก try ให้รวมคำสั่ง except ตามด้วยบล็อกโค้ดที่จัดการปัญหาอย่างเหมาะสมที่สุด
ไวยากรณ์ (Syntax)
นี่คือไวยากรณ์ง่ายๆ ของบล็อก try....except...else:
try:
You do your operations here;
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.
นี่คือจุดสำคัญบางจุดเกี่ยวกับไวยากรณ์ข้างต้น:
- คำสั่ง try เดียวสามารถมีหลาย except statements ได้ นี่มีประโยชน์เมื่อบล็อก try มีคำสั่งที่อาจโยนข้อยกเว้นประเภทต่างๆ
- คุณยังสามารถระบุ except clause แบบ generic ได้ ซึ่งจัดการข้อยกเว้นใดๆ
- หลัง except clause(s) คุณสามารถรวม else-clause ได้ โค้ดใน else-block จะดำเนินการหากโค้ดใน try block ไม่ raise ข้อยกเว้น
- else-block เป็นที่เหมาะสำหรับโค้ดที่ไม่ต้องการการปกป้องของ try block
ตัวอย่าง
ตัวอย่างนี้เปิดไฟล์ เขียนเนื้อหาในไฟล์ และออกมาอย่าง graceful เพราะไม่มีปัญหาเลย:
#!/usr/bin/python
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
except IOError:
print "Error: can't find file or read data"
else:
print "Written content in the file successfully"
fh.close()
ผลลัพธ์ (Output)
ผลลัพธ์ดังนี้:
Written content in the file successfully
ตัวอย่าง
ตัวอย่างนี้พยายามเปิดไฟล์ที่คุณไม่มีสิทธิ์เขียน ดังนั้นจึง raise ข้อยกเว้น:
#!/usr/bin/python
try:
fh = open("testfile", "r")
fh.write("This is my test file for exception handling!!")
except IOError:
print "Error: can't find file or read data"
else:
print "Written content in the file successfully"
ผลลัพธ์ (Output)
ผลลัพธ์ดังนี้:
Error: can't find file or read data
Except Clause ไม่มีข้อยกเว้น (The except Clause with No Exceptions)
คุณยังสามารถใช้ except statement โดยไม่กำหนดข้อยกเว้นได้ดังนี้:
try:
You do your operations here;
......................
except:
If there is any exception, then execute this block.
......................
else:
If there is no exception then execute this block.
ประเภทของคำสั่ง try-except นี้จับข้อยกเว้นทั้งหมดที่เกิดขึ้น การใช้ประเภทนี้ไม่ถือเป็น good programming practice เพราะจับข้อยกเว้นทั้งหมดแต่ไม่ทำให้นักเขียนโปรแกรมระบุสาเหตุรากของปัญหาที่อาจเกิดขึ้น
Except Clause หลายข้อยกเว้น (The except Clause with Multiple Exceptions)
คุณยังสามารถใช้ except statement เดียวกันเพื่อจัดการหลายข้อยกเว้นได้ดังนี้:
try:
You do your operations here;
......................
except(Exception1[, Exception2[,...ExceptionN]]]):
If there is any exception from the given exception list,
then execute this block.
......................
else:
If there is no exception then execute this block.
Try-Finally Clause
คุณสามารถใช้บล็อก finally ร่วมกับบล็อก try บล็อก finally เป็นที่วางโค้ดที่ต้องดำเนินการเสมอ ไม่ว่าบล็อก try จะ raise ข้อยกเว้นหรือไม่ ไวยากรณ์ของคำสั่ง try-finally คือ:
try:
You do your operations here;
......................
Due to any exception, this may be skipped.
finally:
This would always be executed.
......................
คุณไม่สามารถใช้ else clause ร่วมกับ finally clause ได้
ตัวอย่าง
#!/usr/bin/python
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
finally:
print "Error: can't find file or read data"
ผลลัพธ์ (Output)
หากคุณไม่มีสิทธิ์เปิดไฟล์ในโหมดเขียน จะได้ผลลัพธ์ดังนี้:
Error: can't find file or read data
ตัวอย่างเดียวกันสามารถเขียนได้สะอาดกว่านี้:
ตัวอย่าง
#!/usr/bin/python
try:
fh = open("testfile", "w")
try:
fh.write("This is my test file for exception handling!!")
finally:
print "Going to close the file"
fh.close()
except IOError:
print "Error: can't find file or read data"
เมื่อข้อยกเว้นถูกโยนในบล็อก try การดำเนินการจะส่งต่อไปยังบล็อก finally ทันที หลังจากคำสั่งทั้งหมดในบล็อก finally ถูกดำเนินการ ข้อยกเว้นจะถูก raise อีกครั้งและจัดการในคำสั่ง except หากมีในเลเยอร์ try-except ถัดไปที่สูงกว่า