python中的异常及异常的处理方法
异常是什么
异常是可以修改程序控制流程的事件。
在 Python 中,异常可以被错误自动触发,也可以由你的代码手动触发。
我们将学习4种处理异常的语句,第一种有两种形式,最后一种是 Python 2.6 和 Python 3.0 中的可选扩展。
try/except:捕捉并恢复 Python 自动触发的或自己代码中的异常。
try/finally:无论异常是否发生,执行清理操作。
raise:手动触发一个异常。
with/as:在 Python 2.6 ,3.0 或更新的版本中实现上下文管理器。
try/except 语句
try: statements # Run this main action first except name1: # Run if name1 is raised during try block statements except (name2, name3): # Run if any of these exceptions occur statements except name4 as var: # Run if name4 is raised, assign instance raised to var statements except: # Run for all other exceptions raised statements else: statements # Run if no exception was raised during try block
list_of_numbers = [number for number in range(1, 100)] print(list_of_numbers)
dictionary_of_numbers = {} for number in list_of_numbers: dictionary_of_numbers[number**2] = number try: index = list_of_numbers.index(2) value = dictionary_of_numbers[index] except (ValueError, KeyError): print('Error Raised, but Controlled! ') else: # This executes ONLY if no exception is raised print('Getting number at position %d : %d' % (index, value)) finally: # Do cleanup operations print('Cleaning UP')
try/finally 语句
try/finally 是 try 语句的一种形式,finally 语句是 try 之后无论是否出现异常都要执行的语句。
try: statements # Run this action first finally: statements # Always run this code on the way out
with/as 上下文管理器
Python 2.6 和 3.0 引入了一个新的异常相关的语句-with 和可选的 as 子句。with语句允许开发者创建上下文管理器,上下文管理器就是允许你可以自动地开始和结束一些事情。例如,你可能想要打开一个文件,然后写入一些内容,最后再关闭文件。这就是上下文管理器中一个最经典的示例。事实上,当你利用with语句打开一个文件时,Python替你自动创建了一个上下文管理器。
上下文管理器简介
基本用法
with expression [as variable]: with-block
经典用法
with open(r'C:miscdata') as myfile: for line in myfile: print(line) # ...more code here...
使用多个上下文管理器
with open('script1.py') as f1, open('script2.py') as f2: for (linenum, (line1, line2)) in enumerate(zip(f1, f2)): if line1 != line2: print('%s %r %r' % (linenum, line1, line2))
工作原理
上下文管理器必须包含 __enter__ 和 __exit__ 方法。
__enter__ 方法被自动调用,如果存在 as 子句,返回值就被赋值给 as 后的变量,没有就直接丢弃。
嵌套在 with 语句下的代码被执行。
如果 with 语句中的代码抛出异常, __exit__(type, value, traceback) 方法就会被调用。参数值是和 sys.exc_info() (Python 内置函数)函数返回值相同的值。如果这个方法返回了一个 false 值,异常就会被重新抛出,否则异常终止。异常重新抛出是以常规方式抛出的,因此在 with 语句外捕获。
如果 with 语句里的代码块没有抛出异常,__exit__ 方法仍旧会被调用,但是参数会被置为 None。
异常用法
class TraceBlock: def message(self, arg): print('running ' + arg) def __enter__(self): print('starting with block') return self def __exit__(self, exc_type, exc_value, exc_tb): if exc_type is None: print('exited normally ') else: print('raise an exception! ' + str(exc_type)) return False # Propagate
with TraceBlock() as action: action.message('test 1') print('reached')
with TraceBlock() as action: action.message('test 2') raise TypeError() print('not reached')
用户自定义异常
class AlreadyGotOne(Exception): pass def gail(): raise AlreadyGotOne()
try: gail() except AlreadyGotOne: print('got exception')
class Career(Exception): def __init__(self, job, *args, **kwargs): super(Career, self).__init__(*args, **kwargs) self._job = job def __str__(self): return 'So I became a waiter of {}'.format(self._job) raise Career('Engineer')
来源:PY学习网:原文地址:https://www.py.cn/article.html