在咱们的程序开发过程当中,须要区分正常过程与异常(非正常)的状况。为了可以处理异常事件,c语言编程中要求使用条件语句进行判断,尤为是咱们每调用一个函数,都须要对其返回值进行检查,这样子就会致使咱们的代码很大一部分都是对异常的处理,致使代码比较难阅读。在Python中,提供了异常对象来做为解决方案。python
1. exception objectc++
Python用异常对象(exception object)来表示异常状况。若是异常对象未被处理或捕捉,程序就会用所谓的回溯(Traceback)终止执行:编程
def div_func(a, b): return (a / b) val = div_func(1, 0) >>> Traceback (most recent call last): File "E:/code_practice/python_exercise/exception_demo.py", line 4, in <module> val = div_func(1, 0) File "E:/code_practice/python_exercise/exception_demo.py", line 2, in div_func return (a / b) ZeroDivisionError: division by zero
从上面的例子能够看出,因为除数为0,致使ZeroDivisionError异常。咱们能够经过对ZeroDivisionError的捕获,避免程序的终止;编程语言
try: val = div_func(1, 0) print(val) except ZeroDivisionError: print('division by zero') >>> division by zero
2.常见的exception object函数
1)Exception: 全部异常的基类,能够表示任意类型的异常spa
2)AttributeError: 特性引用或赋值失败是引起code
3)IOError: 打开不存在的文件或者读写文件失败时引起对象
4)IndexError: 使用序列中不存在的索引时引起索引
5)KeyError: 使用Dict中不存在的key时引起事件
6)NameError: 找不到变量时引起
7)SyntaxError: 代码为错误形式时引起
8)TypeError: 函数应用于错误类型的对象时引起
9)ValueError: 对象赋值为不合适的值时引起
10)ZeroDivisionError: 除0操做时引起
3. 异常的捕获
使用过面向对象的编程语言,如c++, Java等,都是经过try...catch来对异常进行捕获的,那么在Python中也是如此。大体的语法以下:
try: #statement ... except exception1: #handle type of exception1 ... except exception2: #handle type of exception2 ... except: #handle any type of exception ... else: #when no exception occures, excute here ... finally: #whether exception occures or not, it will excute here ...
若是在执行statement代码的过程当中,接收到了异常,此时会终止statement的继续执行,跳转到异常的捕获处理状况,依次判断每个except分支,直到有符合的except object,则会执行对应的异常处理分支,若是都没有符合的分支,则会跳转到执行finally去执行,而且将此异常继续传递给调用者。若是statement代码没有发生异常,则会执行else分支的代码,最后执行finally中的代码。
若是异常没有被捕获处理,则会一直向上层传播,直至有代码对其捕获处理。若是直到最上层都没有捕获处理,则会打印Traceback信息,并终止程序的运行。
大体的处理流程以下:
try->异常->except->finally
try->无异常->else->finally
#未对引起的异常进行捕获处理的状况 list1 = ['a', 'b'] try: print(list1[3]) except KeyError: print('handle KeyError') >>> Traceback (most recent call last): File "E:/code_practice/python_exercise/exception_demo.py", line 13, in <module> print(list1[3]) IndexError: list index out of range
上面引起的是IndexError异常,没有匹配的异常类型,致使异常没有被匹配到。
#使用finally语句 list1 = ['a', 'b'] try: print(list1[3]) except IndexError: print('index out of range') except: print('catch exception') finally: print('exceute finally statement') >>> index out of range exceute finally statement
执行流程: try-->except IndexError-->finally
3.throw exception
在Python中,经过raise来向上抛出一个exception
def change_list(list_arg): try: list_arg[len(list_arg)] = 'end' except IndexError: print('raise IndexError') raise IndexError else: print('no exception') finally: print('exit change_list') change_list(list1) >>> Traceback (most recent call last): File "E:/code_practice/python_exercise/exception_demo.py", line 24, in change_list list_arg[len(list_arg)] = 'end' IndexError: list assignment index out of range During handling of the above exception, another exception occurred: Traceback (most recent call last): File "E:/code_practice/python_exercise/exception_demo.py", line 33, in <module> change_list(list1) File "E:/code_practice/python_exercise/exception_demo.py", line 27, in change_list raise IndexError IndexError