Python异常与错误处理详细讲解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python异常与错误处理详细讲解

soapcmd   2022-12-23 我要评论

基础知识

优先使用异常捕获

LBYL(look before you leap): 在执行一个可能出错的操作时,先做一些关键的条件判断,仅当满足条件时才进行操作。

EAFP(eaiser to ask for forgiveness than permission): 不做事前检查,直接执行操作。

后者更优: 代码简洁,效率更高

try语句常用知识

把更精确的except语句放在前面

异常类派生关系: BaseException --> Exception --> LookupError --> KeyError

父类被捕获后子类就不会再被触发

使用else分支

try except else

else: 仅当try语句块里面没有抛出任何异常时,才执行else分支

和finally不同,假如在try语句块时碰到了return或者break, 中断了本次异常,那么即使代码没抛出任何异常,else分支内的逻辑也不会被执行

而finally里的语句,无论如何都会被执行,哪怕已经执行了return

使用空raise语句

>>> def incr_by_key(d, key):
...     try:
...         d[key] += 1
...     except KeyError:
...         print('here')
...         raise
...
>>> d = {'a': 1}
>>> incr_by_key(d, 'b')
here
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in incr_by_key
KeyError: 'b'
>>> d['c'] += 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'c'

当一个空raise语句出现在except块里时,它会原封不动地重新抛出当前异常

抛出异常,而不是返回错误

使用上下文管理器

__enter__ __exit__

>>> class DummyContext:
...     def __init__(self, name):
...         self.name = name
...     def __enter__(self):
...         return f"{self.name} -- something"
...     def __exit__(self, exc_type, exc_val, exc_db):
...         print("Exiting")
...         return False
...
>>> with DummyContext('foo') as name:
...     print(f'Name: {name}')
...
Name: foo -- something
Exiting

用于替代finally 语句清理资源

__exit__里面清理资源。

此外__exit__也可以用来对异常进行二次处理然后抛出,或是忽略某种异常等等。

用户忽略异常

一般可以捕获异常后pass

但是也可以:

     def __exit__(self, exc_type, exc_val, exc_db):
         if exc_type == SomeException:
             return True
         return False

此外:使用contextlib里面的suppress也可以实现相同的功能

使用contextmanage装饰器

>>> @contextmanager
... def create_con_obj(host, port, timeout=None):
...     conn = create_conn(host, port, timeout=timeout)
...     try:
...         yield conn
...     finally:
...         conn.close()

yield前面的语句会在进入管理器时执行(类似:__enter__

之后的逻辑会在退出管理器时执行(类似:__exit__

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们