如何在現代 Python 中宣告自定義異常?


要在 Python 中重寫內容或將額外的引數傳遞給異常,可以按照以下步驟進行操作

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

這樣,我們就可以將錯誤訊息的字典傳遞到第二個引數,並在需要時稍後對其進行獲取。

更新日期:2019 年 9 月 26 日

165 次瀏覽

啟動你的 職業

透過完成課程獲取認證

開始
廣告
© . All rights reserved.