Skip to content

Instantly share code, notes, and snippets.

@geekKeen
Last active January 31, 2018 03:35
Show Gist options
  • Save geekKeen/dda49cce7695e299c7aa5557609aa84c to your computer and use it in GitHub Desktop.
Save geekKeen/dda49cce7695e299c7aa5557609aa84c to your computer and use it in GitHub Desktop.
异常处理 / 自定义异常
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)
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
@geekKeen
Copy link
Author

geekKeen commented Oct 10, 2017

  1. HTTPException 如何自定义异常
  2. 异常的继承, BadRequestClientDisconnected 两种方式的异常继承方式

@geekKeen
Copy link
Author

warning
注意初始化时 Exception.__init__(self) 的父类初始化方式
如果是 super 上看
super(HTTPException, self).__init__()

@geekKeen
Copy link
Author

类属性的使用方法
如果该类是可以继承的,且有默认的属性, 提出来做类属性 如 code 和 description

@geekKeen
Copy link
Author

HTTP 各状态码的含义

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment