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

Python Exceptions ทั้งหมดเป็น Runtime Errors หรือไม่

ไม่ใช่ทั้งหมด บางอย่างเป็น Syntax Errors

ตัวอย่าง Syntax Error

if True
    print("hello")
else:
    print("world")

ผลลัพธ์

SyntaxError: invalid syntax

การจัดกลุ่ม Errors

  1. Syntax errors - พบตอน parse โปรแกรม ยังไม่รัน
  2. Runtime errors - พบตอน execute (ZeroDivisionError, TypeError, etc.)
  3. Logical errors - โปรแกรมรันได้แต่ผลผิด

Runtime errors ตัวอย่าง: division by zero, incompatible types, undefined identifier, index/key/attribute not found, file not found