|
33 | 33 | " * Implement context managers in Python 2.6, 3.0, and later (optional in 2.5)."
|
34 | 34 | ]
|
35 | 35 | },
|
36 |
| - { |
37 |
| - "cell_type": "markdown", |
38 |
| - "metadata": {}, |
39 |
| - "source": [ |
40 |
| - "## User Defined Exceptions" |
41 |
| - ] |
42 |
| - }, |
43 |
| - { |
44 |
| - "cell_type": "code", |
45 |
| - "execution_count": 1, |
46 |
| - "metadata": { |
47 |
| - "collapsed": true |
48 |
| - }, |
49 |
| - "outputs": [], |
50 |
| - "source": [ |
51 |
| - "class AlreadyGotOne(Exception): \n", |
52 |
| - " pass\n", |
53 |
| - "\n", |
54 |
| - "def gail():\n", |
55 |
| - " raise AlreadyGotOne()" |
56 |
| - ] |
57 |
| - }, |
58 |
| - { |
59 |
| - "cell_type": "code", |
60 |
| - "execution_count": 2, |
61 |
| - "metadata": { |
62 |
| - "collapsed": false |
63 |
| - }, |
64 |
| - "outputs": [ |
65 |
| - { |
66 |
| - "name": "stdout", |
67 |
| - "output_type": "stream", |
68 |
| - "text": [ |
69 |
| - "got exception\n" |
70 |
| - ] |
71 |
| - } |
72 |
| - ], |
73 |
| - "source": [ |
74 |
| - "try:\n", |
75 |
| - " gail()\n", |
76 |
| - "except AlreadyGotOne:\n", |
77 |
| - " print('got exception')" |
78 |
| - ] |
79 |
| - }, |
80 |
| - { |
81 |
| - "cell_type": "code", |
82 |
| - "execution_count": 3, |
83 |
| - "metadata": { |
84 |
| - "collapsed": false |
85 |
| - }, |
86 |
| - "outputs": [ |
87 |
| - { |
88 |
| - "ename": "Career", |
89 |
| - "evalue": "So I became a waiter...", |
90 |
| - "output_type": "error", |
91 |
| - "traceback": [ |
92 |
| - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
93 |
| - "\u001b[0;31mCareer\u001b[0m Traceback (most recent call last)", |
94 |
| - "\u001b[0;32m<ipython-input-3-8d4820e9348e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__str__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34m'So I became a waiter...'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mCareer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
95 |
| - "\u001b[0;31mCareer\u001b[0m: So I became a waiter..." |
96 |
| - ] |
97 |
| - } |
98 |
| - ], |
99 |
| - "source": [ |
100 |
| - "class Career(Exception):\n", |
101 |
| - " def __str__(self): return 'So I became a waiter...'\n", |
102 |
| - " \n", |
103 |
| - "raise Career()" |
104 |
| - ] |
105 |
| - }, |
106 | 36 | {
|
107 | 37 | "cell_type": "markdown",
|
108 | 38 | "metadata": {},
|
|
116 | 46 | "source": [
|
117 | 47 | "```\n",
|
118 | 48 | "try:\n",
|
119 |
| - " statements \n", |
120 |
| - "except name1:\n", |
| 49 | + " statements # Run this main action first\n", |
| 50 | + "except name1: # Run if name1 is raised during try block\n", |
121 | 51 | " statements\n",
|
122 |
| - "except (name2, name3):\n", |
| 52 | + "except (name2, name3): # Run if any of these exceptions occur\n", |
123 | 53 | " statements \n",
|
124 |
| - "except name4 as var:\n", |
| 54 | + "except name4 as var: # Run if name4 is raised, assign instance raised to var \n", |
125 | 55 | " statements\n",
|
126 |
| - "except:\n", |
| 56 | + "except: # Run for all other exceptions raised\n", |
127 | 57 | " statements\n",
|
128 | 58 | "else:\n",
|
129 |
| - " statements\n", |
130 |
| - "\n", |
131 |
| - "# Run this main action first\n", |
132 |
| - "# Run if name1 is raised during try block\n", |
133 |
| - "# Run if any of these exceptions occur\n", |
134 |
| - "# Run if name4 is raised, assign instance raised to var # Run for all other exceptions raised\n", |
135 |
| - "# Run if no exception was raised during try block\n", |
| 59 | + " statements # Run if no exception was raised during try block\n", |
136 | 60 | "```"
|
137 | 61 | ]
|
138 | 62 | },
|
|
330 | 254 | " raise TypeError()\n",
|
331 | 255 | " print('not reached')"
|
332 | 256 | ]
|
| 257 | + }, |
| 258 | + { |
| 259 | + "cell_type": "markdown", |
| 260 | + "metadata": {}, |
| 261 | + "source": [ |
| 262 | + "## User Defined Exceptions" |
| 263 | + ] |
| 264 | + }, |
| 265 | + { |
| 266 | + "cell_type": "code", |
| 267 | + "execution_count": 1, |
| 268 | + "metadata": { |
| 269 | + "collapsed": true |
| 270 | + }, |
| 271 | + "outputs": [], |
| 272 | + "source": [ |
| 273 | + "class AlreadyGotOne(Exception): \n", |
| 274 | + " pass\n", |
| 275 | + "\n", |
| 276 | + "def gail():\n", |
| 277 | + " raise AlreadyGotOne()" |
| 278 | + ] |
| 279 | + }, |
| 280 | + { |
| 281 | + "cell_type": "code", |
| 282 | + "execution_count": 2, |
| 283 | + "metadata": { |
| 284 | + "collapsed": false |
| 285 | + }, |
| 286 | + "outputs": [ |
| 287 | + { |
| 288 | + "name": "stdout", |
| 289 | + "output_type": "stream", |
| 290 | + "text": [ |
| 291 | + "got exception\n" |
| 292 | + ] |
| 293 | + } |
| 294 | + ], |
| 295 | + "source": [ |
| 296 | + "try:\n", |
| 297 | + " gail()\n", |
| 298 | + "except AlreadyGotOne:\n", |
| 299 | + " print('got exception')" |
| 300 | + ] |
| 301 | + }, |
| 302 | + { |
| 303 | + "cell_type": "code", |
| 304 | + "execution_count": 2, |
| 305 | + "metadata": { |
| 306 | + "collapsed": false, |
| 307 | + "scrolled": true |
| 308 | + }, |
| 309 | + "outputs": [ |
| 310 | + { |
| 311 | + "ename": "Career", |
| 312 | + "evalue": "So I became a waiter of Engineer", |
| 313 | + "output_type": "error", |
| 314 | + "traceback": [ |
| 315 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 316 | + "\u001b[0;31mCareer\u001b[0m Traceback (most recent call last)", |
| 317 | + "\u001b[0;32m<ipython-input-2-d663e503fce1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34m'So I became a waiter of {}'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_job\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mCareer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Engineer'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
| 318 | + "\u001b[0;31mCareer\u001b[0m: So I became a waiter of Engineer" |
| 319 | + ] |
| 320 | + } |
| 321 | + ], |
| 322 | + "source": [ |
| 323 | + "class Career(Exception):\n", |
| 324 | + " \n", |
| 325 | + " def __init__(self, job, *args, **kwargs):\n", |
| 326 | + " super(Career, self).__init__(*args, **kwargs)\n", |
| 327 | + " self._job = job\n", |
| 328 | + " \n", |
| 329 | + " def __str__(self): \n", |
| 330 | + " return 'So I became a waiter of {}'.format(self._job)\n", |
| 331 | + " \n", |
| 332 | + "raise Career('Engineer')" |
| 333 | + ] |
333 | 334 | }
|
334 | 335 | ],
|
335 | 336 | "metadata": {
|
|
0 commit comments