异常处理

  1. try:
  2. # do something
  3. except 异常类型1:
  4. # do something
  5. except 异常类型2:
  6. # do something
  7. ...
  8. except:
  9. # do something
  10. finally:
  11. # try正常结束或except异常处理结束都会执行

自定义异常

  1. class MyException(Exception):
  2. def __init__(self,message):
  3. super().__init__(message)
  4. try:
  5. pass
  6. except Exception:
  7. raise MyException('出错辣~')