Skip to content

Commit a892a29

Browse files
committed
chapter34 finished
1 parent d9bb001 commit a892a29

File tree

1 file changed

+301
-0
lines changed

1 file changed

+301
-0
lines changed
+301
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 第34章 异常对象"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## 基于类的异常"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"- 类可以让你组织的异常分类,使用和维护起来灵活;另外,类可附加异常的细节,而且支持继承\n",
22+
"- 类异常是由超类关系进行匹配的:只要except子句列举了异常的类或其任何超类名,引发的异常就会匹配该子句"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {},
28+
"source": [
29+
"## 默认打印和状态"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"- 内置异常还提供了默认打印显示和状态保持,它往往和用户定义的类所需的逻辑一样的多;除非你重新定义了类继承自它们的构造函数,传递给这些类的任何构造函数参数都会保存在实例的args元组属性中"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": 1,
42+
"metadata": {},
43+
"outputs": [
44+
{
45+
"ename": "IndexError",
46+
"evalue": "",
47+
"output_type": "error",
48+
"traceback": [
49+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
50+
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
51+
"\u001b[0;32m<ipython-input-1-55a00e7db5b5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mIndexError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
52+
"\u001b[0;31mIndexError\u001b[0m: "
53+
]
54+
}
55+
],
56+
"source": [
57+
"raise IndexError"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 2,
63+
"metadata": {},
64+
"outputs": [
65+
{
66+
"ename": "IndexError",
67+
"evalue": "spam",
68+
"output_type": "error",
69+
"traceback": [
70+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
71+
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
72+
"\u001b[0;32m<ipython-input-2-53b7e456dbfa>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mIndexError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'spam'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
73+
"\u001b[0;31mIndexError\u001b[0m: spam"
74+
]
75+
}
76+
],
77+
"source": [
78+
"raise IndexError('spam')"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 3,
84+
"metadata": {},
85+
"outputs": [
86+
{
87+
"data": {
88+
"text/plain": [
89+
"('spam',)"
90+
]
91+
},
92+
"execution_count": 3,
93+
"metadata": {},
94+
"output_type": "execute_result"
95+
}
96+
],
97+
"source": [
98+
"I = IndexError('spam')\n",
99+
"I.args"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 4,
105+
"metadata": {},
106+
"outputs": [
107+
{
108+
"data": {
109+
"text/plain": [
110+
"['__class__',\n",
111+
" '__delattr__',\n",
112+
" '__dict__',\n",
113+
" '__doc__',\n",
114+
" '__format__',\n",
115+
" '__getattribute__',\n",
116+
" '__getitem__',\n",
117+
" '__getslice__',\n",
118+
" '__hash__',\n",
119+
" '__init__',\n",
120+
" '__new__',\n",
121+
" '__reduce__',\n",
122+
" '__reduce_ex__',\n",
123+
" '__repr__',\n",
124+
" '__setattr__',\n",
125+
" '__setstate__',\n",
126+
" '__sizeof__',\n",
127+
" '__str__',\n",
128+
" '__subclasshook__',\n",
129+
" '__unicode__',\n",
130+
" 'args',\n",
131+
" 'message']"
132+
]
133+
},
134+
"execution_count": 4,
135+
"metadata": {},
136+
"output_type": "execute_result"
137+
}
138+
],
139+
"source": [
140+
"dir(I)"
141+
]
142+
},
143+
{
144+
"cell_type": "markdown",
145+
"metadata": {},
146+
"source": [
147+
"- 对于用户定义的异常也是如此,因为它们继承了其内置超类中存在的构造函数和显示方法"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": 5,
153+
"metadata": {},
154+
"outputs": [
155+
{
156+
"name": "stdout",
157+
"output_type": "stream",
158+
"text": [
159+
"('spam', 'eggs', 'ham')\n",
160+
"('spam', 'eggs', 'ham')\n"
161+
]
162+
}
163+
],
164+
"source": [
165+
"class E(Exception):\n",
166+
" pass\n",
167+
"try:\n",
168+
" raise E('spam', 'eggs', 'ham')\n",
169+
"except E as X:\n",
170+
" print X\n",
171+
" print X.args"
172+
]
173+
},
174+
{
175+
"cell_type": "markdown",
176+
"metadata": {},
177+
"source": [
178+
"- 尽管这种自动状态和现实支持本身是有用的,但对于特定的显示和状态保持需求,你总是可以重新定义Exception子类中的`__str__`和`__init__`这样的继承方法"
179+
]
180+
},
181+
{
182+
"cell_type": "markdown",
183+
"metadata": {},
184+
"source": [
185+
"## 定制打印显示"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": 6,
191+
"metadata": {},
192+
"outputs": [
193+
{
194+
"name": "stdout",
195+
"output_type": "stream",
196+
"text": [
197+
"Hello World\n"
198+
]
199+
}
200+
],
201+
"source": [
202+
"class MyBad(Exception):\n",
203+
" def __str__(self):\n",
204+
" return \"Hello World\"\n",
205+
"try:\n",
206+
" raise MyBad()\n",
207+
"except MyBad as X:\n",
208+
" print X"
209+
]
210+
},
211+
{
212+
"cell_type": "markdown",
213+
"metadata": {},
214+
"source": [
215+
"## 定制数据和行为"
216+
]
217+
},
218+
{
219+
"cell_type": "markdown",
220+
"metadata": {},
221+
"source": [
222+
"### 提供异常细节"
223+
]
224+
},
225+
{
226+
"cell_type": "code",
227+
"execution_count": 7,
228+
"metadata": {},
229+
"outputs": [
230+
{
231+
"name": "stdout",
232+
"output_type": "stream",
233+
"text": [
234+
"Error as spam.txt 42\n"
235+
]
236+
}
237+
],
238+
"source": [
239+
"class FormatError(Exception):\n",
240+
" def __init__(self, line, filename):\n",
241+
" self.line = line\n",
242+
" self.filename = filename\n",
243+
"try:\n",
244+
" raise FormatError(42, 'spam.txt')\n",
245+
"except FormatError as X:\n",
246+
" print \"Error as\", X.filename, X.line"
247+
]
248+
},
249+
{
250+
"cell_type": "markdown",
251+
"metadata": {},
252+
"source": [
253+
"### 提供异常方法"
254+
]
255+
},
256+
{
257+
"cell_type": "code",
258+
"execution_count": 8,
259+
"metadata": {},
260+
"outputs": [
261+
{
262+
"name": "stdout",
263+
"output_type": "stream",
264+
"text": [
265+
"Hello World\n"
266+
]
267+
}
268+
],
269+
"source": [
270+
"class FormatError(Exception):\n",
271+
" def func(self):\n",
272+
" print \"Hello World\"\n",
273+
"try:\n",
274+
" raise FormatError()\n",
275+
"except FormatError as X:\n",
276+
" X.func()"
277+
]
278+
}
279+
],
280+
"metadata": {
281+
"kernelspec": {
282+
"display_name": "Python 2",
283+
"language": "python",
284+
"name": "python2"
285+
},
286+
"language_info": {
287+
"codemirror_mode": {
288+
"name": "ipython",
289+
"version": 2
290+
},
291+
"file_extension": ".py",
292+
"mimetype": "text/x-python",
293+
"name": "python",
294+
"nbconvert_exporter": "python",
295+
"pygments_lexer": "ipython2",
296+
"version": "2.7.10"
297+
}
298+
},
299+
"nbformat": 4,
300+
"nbformat_minor": 2
301+
}

0 commit comments

Comments
 (0)