|
319 | 319 | " print(\"caught an exception)"
|
320 | 320 | ]
|
321 | 321 | },
|
| 322 | + { |
| 323 | + "cell_type": "code", |
| 324 | + "execution_count": 25, |
| 325 | + "metadata": {}, |
| 326 | + "outputs": [ |
| 327 | + { |
| 328 | + "name": "stdout", |
| 329 | + "output_type": "stream", |
| 330 | + "text": [ |
| 331 | + "OS error: [Errno 2] No such file or directory: 'myfile.txt'\n" |
| 332 | + ] |
| 333 | + } |
| 334 | + ], |
| 335 | + "source": [ |
| 336 | + "import sys\n", |
| 337 | + "\n", |
| 338 | + "try:\n", |
| 339 | + " f = open('myfile.txt')\n", |
| 340 | + " s = f.readline()\n", |
| 341 | + " i = int(s.strip())\n", |
| 342 | + "except OSError as err:\n", |
| 343 | + " print(\"OS error: {0}\".format(err))\n", |
| 344 | + "except ValueError:\n", |
| 345 | + " print(\"Could not convert data to an integer.\")\n", |
| 346 | + "except:\n", |
| 347 | + " print(\"Unexpected error:\", sys.exc_info()[0])\n", |
| 348 | + " raise" |
| 349 | + ] |
| 350 | + }, |
| 351 | + { |
| 352 | + "cell_type": "code", |
| 353 | + "execution_count": 26, |
| 354 | + "metadata": {}, |
| 355 | + "outputs": [ |
| 356 | + { |
| 357 | + "name": "stdout", |
| 358 | + "output_type": "stream", |
| 359 | + "text": [ |
| 360 | + "Could not convert data to an integer.\n" |
| 361 | + ] |
| 362 | + } |
| 363 | + ], |
| 364 | + "source": [ |
| 365 | + "import sys\n", |
| 366 | + "\n", |
| 367 | + "try:\n", |
| 368 | + " f = open('file.txt')\n", |
| 369 | + " s = f.readline()\n", |
| 370 | + " i = int(s.strip())\n", |
| 371 | + "except OSError as err:\n", |
| 372 | + " print(\"OS error: {0}\".format(err))\n", |
| 373 | + "except ValueError:\n", |
| 374 | + " print(\"Could not convert data to an integer.\")\n", |
| 375 | + "except:\n", |
| 376 | + " print(\"Unexpected error:\", sys.exc_info()[0])\n", |
| 377 | + " raise" |
| 378 | + ] |
| 379 | + }, |
| 380 | + { |
| 381 | + "cell_type": "code", |
| 382 | + "execution_count": 27, |
| 383 | + "metadata": {}, |
| 384 | + "outputs": [ |
| 385 | + { |
| 386 | + "name": "stdout", |
| 387 | + "output_type": "stream", |
| 388 | + "text": [ |
| 389 | + "cannot open -f\n", |
| 390 | + "/run/user/1000/jupyter/kernel-0150ccef-642d-4c64-83c7-bb242793ea41.json has 12 lines\n" |
| 391 | + ] |
| 392 | + } |
| 393 | + ], |
| 394 | + "source": [ |
| 395 | + "for arg in sys.argv[1:]:\n", |
| 396 | + " try:\n", |
| 397 | + " f = open(arg, 'r')\n", |
| 398 | + " except OSError:\n", |
| 399 | + " print('cannot open', arg)\n", |
| 400 | + " else:\n", |
| 401 | + " print(arg, 'has', len(f.readlines()), 'lines')\n", |
| 402 | + " f.close()" |
| 403 | + ] |
| 404 | + }, |
| 405 | + { |
| 406 | + "cell_type": "code", |
| 407 | + "execution_count": 28, |
| 408 | + "metadata": {}, |
| 409 | + "outputs": [ |
| 410 | + { |
| 411 | + "name": "stdout", |
| 412 | + "output_type": "stream", |
| 413 | + "text": [ |
| 414 | + "('E', 'r', 'r', 'o', 'r')\n" |
| 415 | + ] |
| 416 | + } |
| 417 | + ], |
| 418 | + "source": [ |
| 419 | + "class Networkerror(RuntimeError): \n", |
| 420 | + " def __init__(self, arg): \n", |
| 421 | + " self.args = arg \n", |
| 422 | + " \n", |
| 423 | + "try: \n", |
| 424 | + " raise Networkerror(\"Error\") \n", |
| 425 | + " \n", |
| 426 | + "except Networkerror as e: \n", |
| 427 | + " print (e.args) " |
| 428 | + ] |
| 429 | + }, |
322 | 430 | {
|
323 | 431 | "cell_type": "code",
|
324 | 432 | "execution_count": null,
|
|
0 commit comments