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

วิธี Pass Argument ให้ Exception ใน Python

Exception สามารถรับ argument ได้ (เพิ่ม info) จับได้ด้วย variable ใน except clause

ตัวอย่าง

try:
    b = float(56 + 78/0)
except Exception as Argument:
    print('This is the Argument\n', Argument)

ผลลัพธ์

This is the Argument
integer division or modulo by zero

Single exception: variable หลังชื่อ exception

Multiple exceptions: variable หลัง tuple

Argument มักเป็น tuple: (error string, error number, error location)