Python之异常(try的用法)

1、什么是异常?

程序在运行的时候,如果python解释器遇到一个错误,会停止程序的执行,并且提示一些错误的信息,这就是异常。

我们在程序开发的时候,很难将所有的特殊情况都处理,通过异常捕获可以针对突发事件做集中处理,从而保证程序的健壮性和稳定性。

在程序开发中,如果对某些代码的执行不能确定(程序语法完全正确)可以增加try来捕获异常。


2、异常处理语句 try…except…finally

  1. except 语句不是必须的,finally语句也不是必须的,但是二者必须要有一个,否则就没有try的意义了。
  2. except语句可以有多个,Python会按except语句的顺序依次匹配你指定的异常,如果异常已经处理就不会再进入后面的except语句。
  3. except语句可以以元组形式同时指定多个异常,参见示例代码。
  4. except语句后面如果不指定异常类型,则默认捕获所有异常,你可以通过logging或者sys模块获取当前异常。
  5. 如果要捕获异常后重复抛出,请使用raise,后面不要带任何参数或信息。
  6. 不建议捕获并抛出同一个异常,请考虑重构你的代码。
  7. 不建议在不清楚逻辑的情况下捕获所有异常,有可能你隐藏了很严重的问题。
  8. 尽量使用内置的异常处理语句来替换try/except语句,比如with语句,getattr()方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 简单用法
try:
# 不能确定正确执行的代码
num = int(input('请输入一个数字:'))
except:
print('请输入正确的整数')

print('*' * 50)

####################################################################

# 提示用户输入一个整数,并使用8来除以用户输入的整数
try:
num = int(input('请输入一个整数:'))
result = 8 / num
print(result)
except ZeroDivisionError:
print('0不能做除数')
except ValueError:
print('输入的值不是合法的整数')
except Exception as r:
print('未知错误 %s' %(r))
else:
print('只有当一切顺利时才会执行到此处')
finally:
# 无论是否有异常,都会执行的代码
print("#####################")

####################################################################

# except一行中有多个异常
try:
print(a / b)
except (ZeroDivisionError, TypeError) as e:
print(e)

####################################################################

# except代码块是可选的
try:
open(database)
finally:
close(database)

####################################################################

# 抓住所有错误并log下来
try:
do_work()
except:
# 使用logging模块获得详情
logging.exception('Exception caught!')

# 使用sys.exc_info()函数获得详情
error_type, error_value, trace_back = sys.exc_info()
print(error_value)
raise

3、抛出异常raise

raise关键字后面可以指定你要抛出的异常实例,一般来说抛出的异常越详细越好,Python在exceptions模块内建了很多的异常类型,通过使用dir()函数来查看exceptions中的异常类型,如下:

1
2
3
import exceptions

print dir(exceptions)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 主动抛出异常
def input_passwd():
# 1.提示用户输入密码
pwd = input('请输入密码:')
# 2.判断密码的长度
if len(pwd) >=8:
return pwd
# 3.如果<8就主动抛出异常
print('主动抛出异常')
# 4.主动抛出
raise Exception('密码长度不够')

# 注意:只抛出异常而不捕获异常 代码会出错
try:
print(input_passwd())
except Exception as re:
print(re)

4、自定义异常类型

Python中自定义自己的异常类型非常简单,只需要从 Exception 类继承即可(直接或间接):

1
2
3
4
5
class SomeCustomException(Exception):
pass

class AnotherException(SomeCustomException):
pass

一般在自定义异常类型时,需要考虑的问题应该时这个异常所应用的场景。如果内置异常已经包括了你需要的异常,建议考虑使用内置的异常类型。比如你希望在函数参数错误时抛出一个异常,你可能不需要定义一个 InvalidArgumentError,使用内置的 ValueError即可。


5、传递异常 re-raise Exception

捕捉到了异常,但是又想重新抛出它(传递异常),使用不带参数的raise语句即可。

在Python2中,为了保持异常的完整信息,那么你捕获后再次抛出时千万不能在raise后再加上异常对象,否则你的trace信息就会从此处截断。以上是最简单的重新抛出异常的做法,也是推荐到做法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def f1():
print(1/0)

def f2():
try:
f1()
except Exception as e:
raise # don't raise e !!!
f2()

####################################################################

# 还有一些技巧可以考虑,比如抛出异常前你希望对异常的信息进行更新
def f2():
try:
f1()
except Exception as e:
e.args += ('more info',)
raise
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 异常的传递
def demo1():
return int(input('请输入整数:'))

def demo2():
return demo1()

# 函数的错误:一级一级的去找,最终会将异常传递到主函数里
try:
print(demo2())
except Exception as r:
print('未知错误 %s' %r)
print(demo2())

"""
请输入整数:d
未知错误 invalid literal for int() with base 10: 'd'
请输入整数:k
Traceback (most recent call last):
File "E:\CLion2022.2.3\Project\primertest\Test5.py", line 12, in <module>
print(demo2())
File "E:\CLion2022.2.3\Project\primertest\Test5.py", line 5, in demo2
return demo1()
File "E:\CLion2022.2.3\Project\primertest\Test5.py", line 2, in demo1
return int(input('请输入整数:'))
ValueError: invalid literal for int() with base 10: 'k'
"""

6、使用断言处理异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 断言, 可以理解为提前预言 让人更好的知道错误的原因
def func(num,div):
assert (div != 0), 'div不能为0'
return num/div

print(func(10,0))

"""
Traceback (most recent call last):
File "E:\CLion2022.2.3\Project\primertest\Test5.py", line 5, in <module>
print(func(10,0))
File "E:\CLion2022.2.3\Project\primertest\Test5.py", line 2, in func
assert (div != 0), 'div不能为0'
AssertionError: div不能为0
"""

7、Exception 和 BaseException

当我们要捕获一个通用异常时,应该用 Exception 还是 BaseException ?我建议看一下 官方文档说明 ,这两个异常到底有啥区别?请看它们之间的继承关系。

Exception 的层级结构来看,BaseException 是最基础的异常类,Exception 继承了它。 BaseException 除了包含所有的 Exception 外还包含了 SystemExitKeyboardInterruptGeneratorExit 三个异常。

由此看来你的程序在捕获所有异常时更应该使用 Exception 而不是 BaseException ,因为被排除的三个异常属于更高级别的异常,合理的做法应该是交给Python的解释器处理。

1
2
3
4
5
6
7
8
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration...
+-- StandardError...
+-- Warning...

8、except Exception as e 和 except Exception, e

在Python2时,你可以使用以上两种写法中的任意一种。在Python3中你只能使用第一种写法,第二种写法已经不再支持。第一种写法可读性更好,而且为了程序的兼容性和后期移植的成本,请抛弃第二种写法。

1
2
3
4
5
6
try:
do_something()
except NameError as e: # should
pass
except KeyError, e: # should not
pass

9、raise “Exception string”

把字符串当成异常抛出看上去是一个非常简洁的办法,但其实是一个非常不好的习惯。

1
2
3
4
if is_work_done():
pass
else:
raise "Word is not done!" # not cool

上面的语句如果抛出异常,会是这样:

1
2
3
4
Traceback (most recent call last):
File "/demo/exception_hanlding.py", line 48, in <module>
raise "Work is not done!"
TypeError: exceptions must be old-style classes or derived from BaseException, not str

这在 Python2.4 以前是可以接受的做法,但是没有指定异常类型有可能会让下游没办法正确捕获并处理这个异常,从而导致你的程序难以维护。简单说,这种写法是是封建时代的陋习,应该扔了。


10、使用内置语法范式代替 try/except

Python 本身提供了很多的语法范式简化了异常的处理,比如for语句就处理了的StopIteration异常,让你很流畅地写出一个循环。

with语句在打开文件后会自动调用finally并关闭文件。我们在写 Python 代码时应该尽量避免在遇到这种情况时还使用try/except/finally的思维来处理。

1
2
3
4
5
6
7
8
9
10
# should not
try:
f = open(a_file)
do_something(f)
finally:
f.close()

# should
with open(a_file) as f:
do_something(f)

再比如,当我们需要访问一个不确定的属性时,有可能你会写出这样的代码:

1
2
3
4
5
try:
test = Test()
name = test.name # not sure if we can get its name
except AttributeError:
name = 'default'

其实你可以使用更简单的getattr()来达到你的目的。

1
name = getattr(test, 'name', 'default')

11、最佳实践准则

  1. 只处理你知道的异常,避免捕获所有异常然后吞掉它们。
  2. 抛出的异常应该说明原因,有时候你知道异常类型也猜不出所以然。
  3. 避免在catch语句块中干一些没意义的事情,捕获异常也是需要成本的。
  4. 不要使用异常来控制流程,那样你的程序会无比难懂和难维护。
  5. 如果有需要,切记使用finally来释放资源。
  6. 如果有需要,请不要忘记在处理异常后做清理工作或者回滚操作。