一、什么是异常
异常是错误发生的信号,一旦程序出错,并且程序没有处理这个错误,那么就会抛出异常,并且程序的运行随之终止
例如:
1 print('1') 2 print('2') 3 print('3') 4 int('aaaa') 5 print('4') 6 print('5') 7 print('6') 8 9 结果为: 10 11 Traceback (most recent call last): 12 1 13 2 14 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/面向对象编程/32 异常处理.py", line 6, in <module> 15 3 16 int('aaaa') 17 ValueError: invalid literal for int() with base 10: 'aaaa'
报错后后面的代码没有被执行
二、错误分为两种
1、语法错误
例如:
1 print(xxx 2 3 if 1 > 3 4 5 结果为: 6 7 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/面向对象编程/32 异常处理.py", line 15 8 9 ^ 10 SyntaxError: unexpected EOF while parsing
2、逻辑错误
a、ValueError
1 int('aaa') 2 3 结果为: 4 5 Traceback (most recent call last): 6 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/面向对象编程/32 异常处理.py", line 15, in <module> 7 int('aaa') 8 ValueError: invalid literal for int() with base 10: 'aaa'
b、NameError
1 name 2 3 结果为: 4 5 Traceback (most recent call last): 6 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/面向对象编程/32 异常处理.py", line 17, in <module> 7 name 8 NameError: name 'name' is not defined
c、IndexError
1 l = [1, 2, 3] 2 l[100] 3 4 结果为: 5 6 Traceback (most recent call last): 7 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/面向对象编程/32 异常处理.py", line 20, in <module> 8 l[100] 9 IndexError: list index out of range
d、KeyError
1 d = {} 2 d['name'] 3 4 结果为: 5 6 Traceback (most recent call last): 7 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/面向对象编程/32 异常处理.py", line 23, in <module> 8 d['name'] 9 KeyError: 'name'
e、AttributeError
1 class Foo: 2 pass 3 4 Foo.xxx 5 6 结果为: 7 8 Traceback (most recent call last): 9 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/面向对象编程/32 异常处理.py", line 28, in <module> 10 Foo.xxx 11 AttributeError: type object 'Foo' has no attribute 'xxx'
f、TypeError
1 for i in 3: 2 pass 3 4 结果为: 5 6 Traceback (most recent call last): 7 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/面向对象编程/32 异常处理.py", line 30, in <module> 8 for i in 3: 9 TypeError: 'int' object is not iterable
g、ZeroDivisionError
1 res1 = 1 / 0 2 res2 = 1 + 'str' 3 4 结果为: 5 6 7 Traceback (most recent call last): 8 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/面向对象编程/32 异常处理.py", line 33, in <module> 9 res1 = 1 / 0 10 ZeroDivisionError: division by zero
h、KeyboardInterrupt
1 >>> 2 >>> import time 3 >>> time.sleep(1000) 4 Traceback (most recent call last): 5 File "<stdin>", line 1, in <module> 6 KeyboardInterrupt 7 >>>
三、异常
强调1:错误发生条件如果是可以预知的,此时应该用if判断方法去预防异常
例如:
1 AGE = 10 2 3 age = input('>>: ').strip() 4 age = int(age) 5 if age > AGE: 6 print('too big') 7 8 结果为: 9 10 >>: aaa 11 Traceback (most recent call last): 12 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/面向对象编程/32 异常处理.py", line 47, in <module> 13 age = int(age) 14 ValueError: invalid literal for int() with base 10: 'aaa'
如果输入为非数字就会报错,可以用if判断避免:
1 AGE = 10 2 3 age = input('>>: ').strip() 4 if age.isdigit(): 5 6 age = int(age) 7 if age > AGE: 8 print('too big')
这样就避免抛出异常了
强调2:错误发生的条件如果是不可预知的,此时应该用异常处理机制,try......except
例如:
1 f = open('a.txt') 2 3 print(next(f)) 4 print(next(f)) 5 print(next(f)) 6 print(next(f)) 7 print(next(f)) 8 print(next(f)) 9 print(next(f)) 10 11 f.close() 12 13 结果为: 14 15 111 16 Traceback (most recent call last): 17 18 222 19 20 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/面向对象编程/32 异常处理.py", line 60, in <module> 21 333 22 23 print(next(f)) 24 444 25 26 StopIteration 27 555
不知道执行到什么时候发生异常,且发生异常时后面的代码不会执行,此时使用try.......except
1 try: 2 f = open('a.txt') 3 4 print(next(f)) 5 print(next(f)) 6 print(next(f)) 7 print(next(f)) 8 print(next(f)) 9 print(next(f)) 10 print(next(f)) 11 12 print('after print') 13 f.close() 14 15 except StopIteration: 16 print('出错啦') 17 18 结果为: 19 20 111 21 22 222 23 24 333 25 26 444 27 28 555 29 出错啦
在try except 后面的代码还是可以正常执行的,
1 try: 2 f = open('a.txt') 3 4 print(next(f)) 5 print(next(f)) 6 print(next(f)) 7 print(next(f)) 8 print(next(f)) 9 print(next(f)) 10 print(next(f)) 11 12 print('after print') 13 f.close() 14 15 except StopIteration: 16 print('出错啦') 17 18 print('========1') 19 print('========2') 20 print('========3') 21 22 结果为: 23 24 111 25 26 222 27 28 333 29 30 444 31 32 555 33 出错啦 34 ========1 35 ========2 36 ========3