|
62 | 62 | },
|
63 | 63 | {
|
64 | 64 | "cell_type": "code",
|
65 |
| - "execution_count": 2, |
| 65 | + "execution_count": 9, |
66 | 66 | "metadata": {},
|
67 | 67 | "outputs": [
|
68 | 68 | {
|
|
72 | 72 | "traceback": [
|
73 | 73 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
74 | 74 | "\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
|
75 |
| - "\u001b[1;32m<ipython-input-2-9500656ba1ce>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", |
| 75 | + "\u001b[1;32m<ipython-input-9-9500656ba1ce>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", |
76 | 76 | "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero"
|
77 | 77 | ]
|
78 | 78 | }
|
|
83 | 83 | },
|
84 | 84 | {
|
85 | 85 | "cell_type": "code",
|
86 |
| - "execution_count": 3, |
| 86 | + "execution_count": 10, |
87 | 87 | "metadata": {},
|
88 | 88 | "outputs": [
|
89 | 89 | {
|
|
93 | 93 | "traceback": [
|
94 | 94 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
95 | 95 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
|
96 |
| - "\u001b[1;32m<ipython-input-3-eb3d99f57752>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mspam\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", |
| 96 | + "\u001b[1;32m<ipython-input-10-eb3d99f57752>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mspam\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", |
97 | 97 | "\u001b[1;31mNameError\u001b[0m: name 'spam' is not defined"
|
98 | 98 | ]
|
99 | 99 | }
|
|
104 | 104 | },
|
105 | 105 | {
|
106 | 106 | "cell_type": "code",
|
107 |
| - "execution_count": 4, |
| 107 | + "execution_count": 11, |
108 | 108 | "metadata": {},
|
109 | 109 | "outputs": [
|
110 | 110 | {
|
|
114 | 114 | "traceback": [
|
115 | 115 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
116 | 116 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
|
117 |
| - "\u001b[1;32m<ipython-input-4-f01985f6a385>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'2'\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", |
| 117 | + "\u001b[1;32m<ipython-input-11-f01985f6a385>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'2'\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", |
118 | 118 | "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
|
119 | 119 | ]
|
120 | 120 | }
|
|
123 | 123 | "print('2' + 2)"
|
124 | 124 | ]
|
125 | 125 | },
|
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": 12, |
| 129 | + "metadata": {}, |
| 130 | + "outputs": [ |
| 131 | + { |
| 132 | + "ename": "ValueError", |
| 133 | + "evalue": "substring not found", |
| 134 | + "output_type": "error", |
| 135 | + "traceback": [ |
| 136 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 137 | + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", |
| 138 | + "\u001b[1;32m<ipython-input-12-7e8d2962be5c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mstring0\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'Hello World'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mstring0\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'india'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", |
| 139 | + "\u001b[1;31mValueError\u001b[0m: substring not found" |
| 140 | + ] |
| 141 | + } |
| 142 | + ], |
| 143 | + "source": [ |
| 144 | + "string0='Hello World'\n", |
| 145 | + "string0.index('india')" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": 13, |
| 151 | + "metadata": {}, |
| 152 | + "outputs": [ |
| 153 | + { |
| 154 | + "ename": "IndexError", |
| 155 | + "evalue": "string index out of range", |
| 156 | + "output_type": "error", |
| 157 | + "traceback": [ |
| 158 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 159 | + "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", |
| 160 | + "\u001b[1;32m<ipython-input-13-cb2f44dfef1c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mstring0\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'Hello World'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mstring0\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m12\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", |
| 161 | + "\u001b[1;31mIndexError\u001b[0m: string index out of range" |
| 162 | + ] |
| 163 | + } |
| 164 | + ], |
| 165 | + "source": [ |
| 166 | + "string0='Hello World'\n", |
| 167 | + "string0[12]" |
| 168 | + ] |
| 169 | + }, |
126 | 170 | {
|
127 | 171 | "cell_type": "markdown",
|
128 | 172 | "metadata": {},
|
|
145 | 189 | },
|
146 | 190 | {
|
147 | 191 | "cell_type": "code",
|
148 |
| - "execution_count": 2, |
| 192 | + "execution_count": 18, |
| 193 | + "metadata": {}, |
| 194 | + "outputs": [ |
| 195 | + { |
| 196 | + "name": "stdout", |
| 197 | + "output_type": "stream", |
| 198 | + "text": [ |
| 199 | + "Please enter a number: sdasdasd\n" |
| 200 | + ] |
| 201 | + }, |
| 202 | + { |
| 203 | + "ename": "ValueError", |
| 204 | + "evalue": "invalid literal for int() with base 10: 'sdasdasd'", |
| 205 | + "output_type": "error", |
| 206 | + "traceback": [ |
| 207 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 208 | + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", |
| 209 | + "\u001b[1;32m<ipython-input-18-1cd50bb8690f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;31m# try:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Please enter a number: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[1;31m# break\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;31m# except ValueError:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 210 | + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'sdasdasd'" |
| 211 | + ] |
| 212 | + } |
| 213 | + ], |
| 214 | + "source": [ |
| 215 | + "# while True:\n", |
| 216 | + "\n", |
| 217 | + "x = int(input(\"Please enter a number: \"))\n", |
| 218 | + "y=20\n", |
| 219 | + "print(y**3)\n", |
| 220 | + "print('done')" |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "cell_type": "code", |
| 225 | + "execution_count": 17, |
149 | 226 | "metadata": {},
|
150 | 227 | "outputs": [
|
151 | 228 | {
|
152 | 229 | "name": "stdout",
|
153 | 230 | "output_type": "stream",
|
154 | 231 | "text": [
|
155 |
| - "Please enter a number: a\n", |
| 232 | + "Please enter a number: asdas\n", |
156 | 233 | "invalid literal for int() with base 10: \n",
|
157 | 234 | "Oops! That was no valid number. Try again...\n",
|
158 |
| - "Please enter a number: 1\n" |
| 235 | + "8000\n", |
| 236 | + "done\n" |
159 | 237 | ]
|
160 | 238 | }
|
161 | 239 | ],
|
162 | 240 | "source": [
|
163 |
| - "while True:\n", |
| 241 | + "# while True:\n", |
164 | 242 | " \n",
|
165 |
| - " try:\n", |
166 |
| - " x = int(input(\"Please enter a number: \"))\n", |
167 |
| - " break\n", |
168 |
| - " except ValueError:\n", |
169 |
| - " print('invalid literal for int() with base 10: ')\n", |
170 |
| - " print(\"Oops! That was no valid number. Try again...\")" |
| 243 | + "try:\n", |
| 244 | + " x = int(input(\"Please enter a number: \"))\n", |
| 245 | + "# break\n", |
| 246 | + "except ValueError:\n", |
| 247 | + " print('invalid literal for int() with base 10: ')\n", |
| 248 | + " print(\"Oops! That was no valid number. Try again...\")\n", |
| 249 | + "y=20\n", |
| 250 | + "print(y**3)\n", |
| 251 | + "print('done')" |
171 | 252 | ]
|
172 | 253 | },
|
173 | 254 | {
|
|
297 | 378 | ],
|
298 | 379 | "source": [
|
299 | 380 | "import sys\n",
|
300 |
| - "\n", |
301 | 381 | "try:\n",
|
302 | 382 | " f = open('myfile.txt')\n",
|
303 | 383 | " s = f.readline()\n",
|
304 | 384 | " i = int(s.strip())\n",
|
305 | 385 | "except OSError as err:\n",
|
306 | 386 | " print(\"OS error: {0}\".format(err))\n",
|
| 387 | + " print(\"Unexpected error:\", sys.exc_info()[0],sys.exc_info()[1])\n", |
307 | 388 | "except ValueError:\n",
|
308 | 389 | " print(\"Could not convert data to an integer.\")\n",
|
309 | 390 | "except:\n",
|
310 | 391 | " print(\"Unexpected error:\", sys.exc_info()[0],sys.exc_info()[1])\n",
|
311 | 392 | " raise"
|
312 | 393 | ]
|
313 | 394 | },
|
| 395 | + { |
| 396 | + "cell_type": "code", |
| 397 | + "execution_count": 19, |
| 398 | + "metadata": {}, |
| 399 | + "outputs": [ |
| 400 | + { |
| 401 | + "data": { |
| 402 | + "text/plain": [ |
| 403 | + "(32, 40)" |
| 404 | + ] |
| 405 | + }, |
| 406 | + "execution_count": 19, |
| 407 | + "metadata": {}, |
| 408 | + "output_type": "execute_result" |
| 409 | + } |
| 410 | + ], |
| 411 | + "source": [ |
| 412 | + "# a=10\n", |
| 413 | + "# size(a)\n", |
| 414 | + "\n", |
| 415 | + "import sys\n", |
| 416 | + "# sys.getsizeof(a)\n", |
| 417 | + "a=(1,2,3)\n", |
| 418 | + "b=[1,2,3]\n", |
| 419 | + "sys.getsizeof(a),sys.getsizeof(b)\n" |
| 420 | + ] |
| 421 | + }, |
314 | 422 | {
|
315 | 423 | "cell_type": "markdown",
|
316 | 424 | "metadata": {},
|
|
436 | 544 | "# Handling run-time error: division by zero"
|
437 | 545 | ]
|
438 | 546 | },
|
| 547 | + { |
| 548 | + "cell_type": "code", |
| 549 | + "execution_count": 5, |
| 550 | + "metadata": {}, |
| 551 | + "outputs": [ |
| 552 | + { |
| 553 | + "name": "stdout", |
| 554 | + "output_type": "stream", |
| 555 | + "text": [ |
| 556 | + "*** print_tb:\n", |
| 557 | + " File \"<ipython-input-5-1685af91a914>\", line 9, in <module>\n", |
| 558 | + " lumberjack()\n", |
| 559 | + "*** print_exception:\n", |
| 560 | + "Traceback (most recent call last):\n", |
| 561 | + " File \"<ipython-input-5-1685af91a914>\", line 9, in <module>\n", |
| 562 | + " lumberjack()\n", |
| 563 | + " File \"<ipython-input-5-1685af91a914>\", line 3, in lumberjack\n", |
| 564 | + " bright_side_of_death()\n", |
| 565 | + "IndexError: tuple index out of range\n", |
| 566 | + "*** print_exc:\n", |
| 567 | + "*** format_exc, first and last line:\n", |
| 568 | + "Traceback (most recent call last):\n", |
| 569 | + "IndexError: tuple index out of range\n", |
| 570 | + "*** format_exception:\n", |
| 571 | + "['Traceback (most recent call last):\\n', ' File \"<ipython-input-5-1685af91a914>\", line 9, in <module>\\n lumberjack()\\n', ' File \"<ipython-input-5-1685af91a914>\", line 3, in lumberjack\\n bright_side_of_death()\\n', ' File \"<ipython-input-5-1685af91a914>\", line 6, in bright_side_of_death\\n return tuple()[0]\\n', 'IndexError: tuple index out of range\\n']\n", |
| 572 | + "*** extract_tb:\n", |
| 573 | + "[<FrameSummary file <ipython-input-5-1685af91a914>, line 9 in <module>>, <FrameSummary file <ipython-input-5-1685af91a914>, line 3 in lumberjack>, <FrameSummary file <ipython-input-5-1685af91a914>, line 6 in bright_side_of_death>]\n", |
| 574 | + "*** format_tb:\n", |
| 575 | + "[' File \"<ipython-input-5-1685af91a914>\", line 9, in <module>\\n lumberjack()\\n', ' File \"<ipython-input-5-1685af91a914>\", line 3, in lumberjack\\n bright_side_of_death()\\n', ' File \"<ipython-input-5-1685af91a914>\", line 6, in bright_side_of_death\\n return tuple()[0]\\n']\n", |
| 576 | + "*** tb_lineno: 9\n" |
| 577 | + ] |
| 578 | + }, |
| 579 | + { |
| 580 | + "name": "stderr", |
| 581 | + "output_type": "stream", |
| 582 | + "text": [ |
| 583 | + "Traceback (most recent call last):\n", |
| 584 | + " File \"<ipython-input-5-1685af91a914>\", line 9, in <module>\n", |
| 585 | + " lumberjack()\n", |
| 586 | + " File \"<ipython-input-5-1685af91a914>\", line 3, in lumberjack\n", |
| 587 | + " bright_side_of_death()\n", |
| 588 | + " File \"<ipython-input-5-1685af91a914>\", line 6, in bright_side_of_death\n", |
| 589 | + " return tuple()[0]\n", |
| 590 | + "IndexError: tuple index out of range\n" |
| 591 | + ] |
| 592 | + } |
| 593 | + ], |
| 594 | + "source": [ |
| 595 | + "import sys,traceback\n", |
| 596 | + "def lumberjack():\n", |
| 597 | + " bright_side_of_death()\n", |
| 598 | + "\n", |
| 599 | + "def bright_side_of_death():\n", |
| 600 | + " return tuple()[0]\n", |
| 601 | + "\n", |
| 602 | + "try:\n", |
| 603 | + " lumberjack()\n", |
| 604 | + "except IndexError:\n", |
| 605 | + " exc_type, exc_value, exc_traceback = sys.exc_info()\n", |
| 606 | + " print(\"*** print_tb:\")\n", |
| 607 | + " traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)\n", |
| 608 | + " print(\"*** print_exception:\")\n", |
| 609 | + " traceback.print_exception(exc_type, exc_value, exc_traceback,\n", |
| 610 | + " limit=2, file=sys.stdout)\n", |
| 611 | + " print( \"*** print_exc:\")\n", |
| 612 | + " traceback.print_exc()\n", |
| 613 | + " print( \"*** format_exc, first and last line:\")\n", |
| 614 | + " formatted_lines = traceback.format_exc().splitlines()\n", |
| 615 | + " print( formatted_lines[0])\n", |
| 616 | + " print( formatted_lines[-1])\n", |
| 617 | + " print( \"*** format_exception:\")\n", |
| 618 | + " print( repr(traceback.format_exception(exc_type, exc_value,\n", |
| 619 | + " exc_traceback)))\n", |
| 620 | + " print( \"*** extract_tb:\")\n", |
| 621 | + " print( repr(traceback.extract_tb(exc_traceback)))\n", |
| 622 | + " print( \"*** format_tb:\")\n", |
| 623 | + " print( repr(traceback.format_tb(exc_traceback)))\n", |
| 624 | + " print( \"*** tb_lineno:\", exc_traceback.tb_lineno)" |
| 625 | + ] |
| 626 | + }, |
439 | 627 | {
|
440 | 628 | "cell_type": "markdown",
|
441 | 629 | "metadata": {},
|
|
847 | 1035 | "name": "python",
|
848 | 1036 | "nbconvert_exporter": "python",
|
849 | 1037 | "pygments_lexer": "ipython3",
|
850 |
| - "version": "3.7.1" |
| 1038 | + "version": "3.8.5" |
851 | 1039 | }
|
852 | 1040 | },
|
853 | 1041 | "nbformat": 4,
|
|
0 commit comments