Skip to content

Commit 863555f

Browse files
committed
python exception
1 parent 410cc37 commit 863555f

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
layout: post
3+
title: Python中的异常处理
4+
no-post-nav: true
5+
category: it
6+
tags: [it]
7+
keywords: Exception
8+
excerpt: 没有绝对的异常,只有人为的错误。
9+
---
10+
# Python中的异常处理
11+
12+
## 错误
13+
14+
- 语法上的
15+
- 逻辑上的
16+
- 遇到错误后,解释器会引发异常。如果异常对象并未被处理或捕捉,程序就会用所谓的回溯(traceback)终止执行。
17+
18+
## 异常处理语句 try...except...finally
19+
20+
- `except`语句不是必须的,`finally`语句也不是必须的,但是二者必须要有一个,否则就没有`try`的意义了。
21+
22+
- `except`语句可以有多个,Python会按`except`语句的顺序依次匹配你指定的异常,如果异常已经处理就不会再进入后面的`except`语句。
23+
24+
- `except`语句可以以元组形式同时指定多个异常。
25+
26+
- `except`语句后面如果不指定异常类型,则默认捕获所有异常,你可以通过logging或者sys模块获取当前异常。
27+
28+
```python
29+
import logging
30+
try:
31+
do_work()
32+
except:
33+
# get detail from logging module
34+
logging.exception('Exception caught!')
35+
36+
# get detail from sys.exc_info() method
37+
error_type, error_value, trace_back = sys.exc_info()
38+
print(error_value)
39+
raise
40+
```
41+
42+
- 如果要捕获异常后要重复抛出,请使用`raise`,后面不要带任何参数或信息。
43+
44+
- 不建议捕获并抛出同一个异常,请考虑重构你的代码。
45+
46+
- 不建议在不清楚逻辑的情况下捕获所有异常,有可能你隐藏了很严重的问题。
47+
48+
- 尽量使用内置的异常处理语句来替换`try/except`语句,比如`with`语句,`getattr()`方法。
49+
50+
## 抛出异常
51+
52+
​使用__raise__关键字可自主抛出一个异常,等同于Java和C#中的throw;
53+
54+
```python
55+
raise NameError("bad name!")
56+
```
57+
58+
`raise`关键字后面可以指定你要抛出的异常实例,一般来说抛出的异常越详细越好,Python在`exceptions`模块内建了很多的异常类型,通过使用`dir()`函数来查看`exceptions`中的异常类型,如下:
59+
60+
```python
61+
import exceptions
62+
63+
print dir(exceptions)
64+
# ['ArithmeticError', 'AssertionError'...]
65+
```
66+
67+
## 自定义异常类型
68+
69+
直接从Exception类继承即可。
70+
71+
##异常处理时常见的问题
72+
73+
###Exception和BaseException
74+
75+
当要捕获一个通用异常时,应该用`Exception`还是`BaseException`?以下是它们之间的继承关系。
76+
77+
```python
78+
BaseException
79+
+-- SystemExit
80+
+-- KeyboardInterrupt
81+
+-- GeneratorExit
82+
+-- Exception
83+
+-- StopIteration...
84+
+-- StandardError...
85+
+-- Warning...
86+
```
87+
88+
程序在捕获所有异常时更应该使用`Exception`而不是`BaseException`,因为被排除的三个异常属于更高级别的异常,合理的做法应该是交给Python的解释器处理。
89+
90+
### 使用内置的语法范式代替try/except
91+
92+
Python 本身提供了很多的语法范式简化了异常的处理,比如`for`语句就处理了的`StopIteration`异常,让你很流畅地写出一个循环。
93+
94+
`with`语句在打开文件后会自动调用`finally`并关闭文件。我们在写 Python 代码时应该尽量避免在遇到这种情况时还使用try/except/finally的思维来处理。
95+
96+
```
97+
# should not
98+
try:
99+
f = open(a_file)
100+
do_something(f)
101+
finally:
102+
f.close()
103+
104+
# should
105+
with open(a_file) as f:
106+
do_something(f)
107+
```
108+
109+
当我们需要访问一个不确定的属性时,有可能你会写出这样的代码:
110+
111+
```python
112+
try:
113+
test = Test()
114+
name = test.name # not sure if we can get its name
115+
except AttributeError:
116+
name = 'default'
117+
```
118+
119+
其实我们可以使用更简单的`getattr()`来达到目的。
120+
121+
```python
122+
name = getattr(test, 'name', 'default')
123+
```
124+
125+
## 异常速查表
126+
127+
![Exception_table](G:\image\Exception_table.jpg)
128+
129+
## 异常处理好习惯
130+
131+
- 只处理你知道的异常,尽量避免捕获所有异常然后吞掉它们。
132+
- 抛出的异常应该说明原因,有时候你知道异常类型也猜不出所以然。
133+
- 不要使用异常来控制流程,那样你的程序会无比难懂和难维护。
134+
- 如果有需要,切记使用`finally`来释放资源。
135+
- 如果有需要,请不要忘记在处理异常后做清理工作或者回滚操作。
136+
137+
注:以上内容摘自betacat 《[总结:Python中的异常处理](https://segmentfault.com/a/1190000007736783)》,内容有些许删改。

0 commit comments

Comments
 (0)