Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách declare custom exceptions trong modern Python?

Để override cái gì đó hoặc pass extra arguments cho exception, chúng ta làm như sau trong modern Python:

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

Cách đó chúng ta có thể pass dictionary of error messages cho tham số thứ hai, và truy cập nó sau khi cần thiết.