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

วิธีใช้ Except Clause โดยไม่ระบุ Exception ใน Python

except: โดยไม่ระบุ exception จะ catch ทุกอย่าง (bare except)

ตัวอย่าง

try:
    print('foo' + 'qux' + 7)
except:
    print('There is error')

ผลลัพธ์

There is error

ไม่แนะนำ: ไม่รู้ว่าเป็น exception อะไร ไม่ช่วย debugging

จับ BaseException รวมถึง KeyboardInterrupt, SystemExit ด้วย