Skip to content

Commit 6bb9d7d

Browse files
committed
chapter32 finished
1 parent 3f5650e commit 6bb9d7d

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 第32章 异常基础"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"- 异常可以改变程序的控制流程,它可以根据错误自动地被触发,也能由代码触发和截获,异常语句主要有以下几种:\n",
15+
"```python\n",
16+
"try/except\n",
17+
" 捕捉由Python或你引起的异常并恢复\n",
18+
"try/finally\n",
19+
" 无论异常是否发生,执行清理行为\n",
20+
"raise\n",
21+
" 手动在代码中触发异常\n",
22+
"assert\n",
23+
" 有条件地在代码中触发异常\n",
24+
"with/as\n",
25+
" 环境管理器\n",
26+
"```"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"metadata": {},
32+
"source": [
33+
"## 引发异常"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {},
39+
"source": [
40+
"- 异常能由Python或程序引发"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 1,
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"name": "stdout",
50+
"output_type": "stream",
51+
"text": [
52+
"got exception\n"
53+
]
54+
}
55+
],
56+
"source": [
57+
"try:\n",
58+
" raise IndexError\n",
59+
"except:\n",
60+
" print \"got exception\""
61+
]
62+
},
63+
{
64+
"cell_type": "markdown",
65+
"metadata": {},
66+
"source": [
67+
"- assert也可以用来触发异常——它是一个有条件的raise,主要是在开发过程中用于调试"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": 2,
73+
"metadata": {},
74+
"outputs": [
75+
{
76+
"ename": "AssertionError",
77+
"evalue": "Nobody expects the Spanish Inquisition!",
78+
"output_type": "error",
79+
"traceback": [
80+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
81+
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
82+
"\u001b[0;32m<ipython-input-2-1e7b91bac7dd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Nobody expects the Spanish Inquisition!\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
83+
"\u001b[0;31mAssertionError\u001b[0m: Nobody expects the Spanish Inquisition!"
84+
]
85+
}
86+
],
87+
"source": [
88+
"assert False, \"Nobody expects the Spanish Inquisition!\""
89+
]
90+
},
91+
{
92+
"cell_type": "markdown",
93+
"metadata": {},
94+
"source": [
95+
"## 用户定义的异常"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 3,
101+
"metadata": {},
102+
"outputs": [
103+
{
104+
"name": "stdout",
105+
"output_type": "stream",
106+
"text": [
107+
"got Bad\n"
108+
]
109+
}
110+
],
111+
"source": [
112+
"class Bad(Exception):\n",
113+
" pass\n",
114+
"def doomed():\n",
115+
" raise Bad()\n",
116+
"try:\n",
117+
" doomed()\n",
118+
"except Bad:\n",
119+
" print \"got Bad\""
120+
]
121+
}
122+
],
123+
"metadata": {
124+
"kernelspec": {
125+
"display_name": "Python 2",
126+
"language": "python",
127+
"name": "python2"
128+
},
129+
"language_info": {
130+
"codemirror_mode": {
131+
"name": "ipython",
132+
"version": 2
133+
},
134+
"file_extension": ".py",
135+
"mimetype": "text/x-python",
136+
"name": "python",
137+
"nbconvert_exporter": "python",
138+
"pygments_lexer": "ipython2",
139+
"version": "2.7.10"
140+
}
141+
},
142+
"nbformat": 4,
143+
"nbformat_minor": 2
144+
}

0 commit comments

Comments
 (0)