Python try...except statement จับ exception (ข้อยกเว้น) ใช้ทดสอบโค้ดสำหรับข้อผิดพลาดที่เขียนใน "try" statement หากพบข้อผิดพลาด เนื้อหาของ "except" block จะถูก execute
วิธีใช้ Try และ Except ใน Python
คุณอาจต้องการทดสอบบล็อกโค้ดเฉพาะเพื่อให้แน่ใจว่าทำงานถูกต้องก่อนให้ส่วนอื่นของโปรแกรมทำงาน ตัวอย่างเช่น คุณเขียนโค้ดใหม่จำนวนมากสำหรับโปรแกรม คุณต้องการให้แน่ใจว่ามันทำงานก่อนปล่อยให้โปรแกรมอื่นๆ รัน
try...except blocks ให้คุณทดสอบโค้ดและจัดการ exception หากมีการ raise ขึ้น คุณสามารถเพิ่ม finally และ else statements เพื่อรันโค้ดเพิ่มเติมขึ้นอยู่กับผลลัพธ์ของ try...except block
ในบทช่วยสอนนี้ เราจะพูดถึงวิธีใช้ try และ except ใน Python พร้อมอ้างอิงตัวอย่างเพื่อให้คุณเริ่มใช้ try และ except ได้อย่างรวดเร็ว
Python Errors and Exceptions Refresher (ทบทวน Errors และ Exceptions)
ใน Python มีข้อผิดพลาด 2 ชนิดที่อาจพบ: syntax errors และ exceptions
Python syntax errors คือข้อผิดพลาดที่เกิดจากการใช้ syntax ผิด ตัวอย่างเช่น เขียน while True loop โดยไม่มี colon ท้ายบรรทัด โปรแกรมจะรายงาน error
เมื่อ syntax errors เกิดขึ้น จะคืนค่า file name, line number, และตัวบ่งชี้ตำแหน่งที่อาจมี error
Exceptions คือข้อผิดพลาดที่โค้ดอาจมี syntax ถูกต้องแต่ยังมีปัญหา มีหลายประเภทของ exception แต่ที่พบบ่อยที่สุด ได้แก่: ArithmeticError, ImportError, ZeroDivisionError, NameError, และ TypeError
Python try...except Statement
Python try...except statement รันโค้ดภายใต้ "try" statement หากโค้ดนี้ execute ไม่สำเร็จ โปรแกรมจะหยุดที่บรรทัดที่ทำให้ error และโค้ด "except" จะรัน
Try block ให้คุณทดสอบบล็อกโค้ดสำหรับ errors Except block ให้คุณจัดการ error ด้วย response ที่คุณกำหนดเอง
Syntax สำหรับ try...except block:
try:
yourcode...
except:
yourcode...
คุณสามารถใส่ Python code ที่ถูกต้องใดๆ ไว้ใน try หรือ except statement ได้
try...except Python Example
ตัวอย่าง syntax สำหรับ try...except blocks:
try:
print(ourVariable)
except:
print('Error returned')
ในตัวอย่าง เรายังไม่ได้宣告 Python variable ourVariable แต่พยายามใช้มันใน try block
หากไม่มี try...except blocks โปรแกรมจะคืนค่า error message แม้ error message จะเหมาะสมตอน debugging แต่ผู้ใช้ทั่วไปอาจสับสนหากเห็น error message
เพราะเรามี try...except blocks โค้ดจึงรู้ว่าต้องทำอย่างไรเมื่อเจอ error
ผลลัพธ์ของโค้ด:
Error returned
try...except blocks ให้คุณจัดการ exceptions อย่าง graceful คุณอาจต้องการ implement feature เช่น บันทึก exception ลง log file ด้วย package เช่น Python logging module เพื่อติดตาม exceptions ที่ถูก raise
try...except Python: Multiple Except Statements (หลาย except statements)
คุณสามารถใช้ except statements ซ้ำสำหรับประเภท errors ต่างๆ เพื่อทดสอบ multiple exceptions นี่มีประโยชน์หากคุณสงสัยว่าอาจเจอ exception หลายประเภทแต่ไม่แน่ใจว่าจะเจออันไหน
ตัวอย่าง try...except blocks ที่ค้นหา NameError:
try:
print(ourVariable)
except NameError:
print('ourVariable is not defined')
except:
print('Error returned')
ในกรณีนี้ โค้ดคืนค่า ourVariable is not defined เพราะโค้ดคืนค่า NameError เราสามารถเพิ่ม errors อื่นๆ ได้ เช่น ZeroDivisionError หรือ OSError ขึ้นอยู่กับโค้ดที่ทดสอบ
ตัวอย่าง คุณอาจเช็ค IOError และ FileNotFoundError หากต้องการเปิดไฟล์ การเช็ค multiple exceptions จะทำให้โปรแกรมทำงานต่อได้แม้มี error เปิดไฟล์ที่อ้างอิง
try...except Python: Finally
แต่ถ้าต้องการให้ message พิมพ์ทั้งกรณีมี error และไม่มี error? นี่คือที่มาของ block finally หากคุณกำหนด finally clause เนื้อหาของมันจะถูก execute ไม่ว่าจะมี error ใน try...except block หรือไม่
Finally blocks เป็น indicator ที่มีประโยชน์ว่าโค้ดของคุณ execute แล้ว เพราะไม่แยกแยะว่า code execute สำเร็จหรือไม่ จึงไม่ค่อยใช้บ่อย
ตัวอย่าง:
try:
print(ourVariable)
except:
print('ourVariable is not defined')
finally:
print('Code has been run.')
โปรแกรมคืนค่า:
ourVariable is not defined Code has been run.
โค้ดใน except block execute เพราะมี exception ในโค้ด (ourVariable is not defined) โค้ดใน finally clause ก็ execute เพราะโค้ดรันเสร็จแล้ว
try...except Python: Else
ด้วย else clause คุณสามารถกำหนดโค้ดที่จะรันในกรณีที่ไม่มี exceptions ถูก raise นี่อาจใช้แจ้งผู้ใช้ว่าโปรแกรม execute สำเร็จ
ลองนึกภาพว่าคุณสร้างระบบสมัครสมาชิกเกม คุณอาจใส่ try...except...else block เพื่อเช็คว่า username หรือ email ที่ผู้ใช้เลือก valid หรือไม่ หากไม่ valid except clause จะรัน หาก valid else block จะรัน
ตัวอย่าง:
try:
print('Test')
except:
print('There is a problem.')
else:
print('There are no problems.')
โค้ดคืนค่า:
There are no problems.
Python program ไม่เจอ exceptions ดังนั้นโค้ดใน else statement execute else statement พิมพ์ข้อความว่าไม่มีปัญหากับโค้ด
View the Repl.it from this tutorial:
สรุป
try...except blocks ทำให้ debug Python code ง่ายขึ้น โปรแกรมพยายามรันโค้ดใน "try" block หากล้มเหลว "except" block จะรัน โค้ดใน "finally" statement รันไม่ว่าจะมี "except" block execute หรือไม่
ในบทช่วยสอนนี้ เราแยกวิเคราะห์การใช้ try...except blocks เราอภิปรายการใช้ else และ except เพื่อปรับแต่ง exception handling
Blocks เหล่านี้มีประโยชน์เมื่อทดสอบโค้ดที่มีอยู่หรือเขียนโค้ดใหม่ 确保ว่าโปรแกรมรันถูกต้องและไม่มี errors
สำหรับ Python learning resources เพิ่มเติม ดูที่ How to Learn Python guide