Skip to content

Commit d9bb001

Browse files
committed
chapter33 finished
1 parent 6bb9d7d commit d9bb001

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 第33章 异常编码细节"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"- 空的except子句是一种通用功能,可以捕捉任何异常,比较方便,但是也可能存在问题:可能捕捉和程序代码无关、意料之外的系统异常,而且可能以外拦截其它处理器的异常,因此要小心使用\n",
15+
"```python\n",
16+
"try:\n",
17+
" action()\n",
18+
"except:\n",
19+
" ...\n",
20+
"```"
21+
]
22+
},
23+
{
24+
"cell_type": "markdown",
25+
"metadata": {},
26+
"source": [
27+
"## raise语句"
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"metadata": {},
33+
"source": [
34+
"- 要显式地触发异常,可使用raise语句,其一般形式如下:\n",
35+
"```python\n",
36+
"raise <instance> # 一般而言,我们总是提供一个类的实例\n",
37+
"raise <class> # 隐式创建实例,等同于在类后加上圆括号\n",
38+
"raise # 重新引发最近的异常\n",
39+
"```"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {},
45+
"source": [
46+
"- raise语句不包括异常名称或额外数据值时,就是重新引发当前异常,如果需要捕捉和处理一个异常,又不希望异常在程序代码中死掉时,一般就会使用这种形式;通过这种方式执行raise时,会重新引发异常,并将其传递给更高层的处理器(或者顶层的默认处理器,它会停止程序,打印标准出错信息)\n",
47+
"```python\n",
48+
"try:\n",
49+
" raise IndexError()\n",
50+
"except:\n",
51+
" print \"propagating\"\n",
52+
" raise\n",
53+
"```"
54+
]
55+
},
56+
{
57+
"cell_type": "markdown",
58+
"metadata": {},
59+
"source": [
60+
"## assert语句"
61+
]
62+
},
63+
{
64+
"cell_type": "markdown",
65+
"metadata": {},
66+
"source": [
67+
"- assert可视为条件式的raise语句,该语句形式为`assert <test>, <data>`,执行起来等同于以下代码:\n",
68+
"```python\n",
69+
"if __debug__:\n",
70+
" if not <test>:\n",
71+
" raise AssertionError(<data>)\n",
72+
"```"
73+
]
74+
},
75+
{
76+
"cell_type": "markdown",
77+
"metadata": {},
78+
"source": [
79+
"- assert几乎都是用来收集用户定义的约束条件,而不是捕捉内在的程序设计错误,因为Python会自行处理后者"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": 1,
85+
"metadata": {},
86+
"outputs": [],
87+
"source": [
88+
"# 合理\n",
89+
"def f(x):\n",
90+
" assert x < 0, \"x must be negative\"\n",
91+
"\n",
92+
"# 不合理,Python会在遇见错误时自动引发异常\n",
93+
"def f2(x):\n",
94+
" assert x != 0\n",
95+
" return 1 / x"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"metadata": {},
101+
"source": [
102+
"## with/as环境管理器"
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"metadata": {},
108+
"source": [
109+
"### 基本使用"
110+
]
111+
},
112+
{
113+
"cell_type": "markdown",
114+
"metadata": {},
115+
"source": [
116+
"- with语句的基本格式如下:\n",
117+
"```python\n",
118+
"with expression [as variable]:\n",
119+
" with-block\n",
120+
"```\n",
121+
"- with不仅仅可用于文件的打开,也可用于锁的自动上锁和开锁:\n",
122+
"```python\n",
123+
"lock = threading.Lock()\n",
124+
"with lock:\n",
125+
" ...access shared resources...\n",
126+
"```"
127+
]
128+
}
129+
],
130+
"metadata": {
131+
"kernelspec": {
132+
"display_name": "Python 2",
133+
"language": "python",
134+
"name": "python2"
135+
},
136+
"language_info": {
137+
"codemirror_mode": {
138+
"name": "ipython",
139+
"version": 2
140+
},
141+
"file_extension": ".py",
142+
"mimetype": "text/x-python",
143+
"name": "python",
144+
"nbconvert_exporter": "python",
145+
"pygments_lexer": "ipython2",
146+
"version": "2.7.10"
147+
}
148+
},
149+
"nbformat": 4,
150+
"nbformat_minor": 2
151+
}

0 commit comments

Comments
 (0)