Last active
January 31, 2018 03:35
-
-
Save geekKeen/dda49cce7695e299c7aa5557609aa84c to your computer and use it in GitHub Desktop.
异常处理 / 自定义异常
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JobLookupError(KeyError): | |
def __init__(self, job_id): | |
super(KeyError, self).__init__(u'No job by the id of %s was found' % job_id) | |
raise JobLookupError(job_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HTTPException(Exception): | |
code = None | |
description = None | |
def __init__(self, description=None, response=None): | |
Exception.__init__(self) | |
if description is not None: | |
self.description = description | |
self.response = response | |
class BadRequest(HTTPException): | |
code = 400 | |
description = '...' | |
class ClientDisconnected(BadRequest): | |
pass |
warning
注意初始化时 Exception.__init__(self)
的父类初始化方式
如果是 super
上看
super(HTTPException, self).__init__()
类属性的使用方法
如果该类是可以继承的,且有默认的属性, 提出来做类属性 如 code 和 description
HTTP 各状态码的含义
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HTTPException
如何自定义异常BadRequest
和ClientDisconnected
两种方式的异常继承方式