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

ความแตกต่างระหว่าง except Exception as e และ except Exception, e ใน Python

Functionality เหมือนกัน แตกต่างที่ Python version

  • Python 2.5 และก่อน: ใช้ comma except Exception, e: (as ยังไม่รองรับ)
  • Python 2.6+: ใช้ทั้ง comma และ as ได้
  • Python 3.x: ต้องใช้ as เท่านั้น

ข้อดีของ as: รองรับ multiple exceptions สวยงาม

except (Exception1, Exception2) as err  # สวยกว่า
except (Exception1, Exception2), err    # เก่า