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

วิธี Declare Custom Exceptions ใน Modern Python

Modern Python (3.x) ใช้ super() และเก็บ extra arguments

ตัวอย่าง

class ValidationError(Exception):
    def __init__(self, message, errors):
        super(ValidationError, self).__init__(message)
        self.errors = errors

สามารถส่ง dictionary ของ error messages ไปใน parameter ที่สอง และเข้าถึงได้ภายหลัง