{ "metadata": { "name": "", "signature": "sha256:91de08b1208f394408cba80689733abdbcde56c09bf58b5b9eac587d3d51a088" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "***\n", "# 13. \uc608\uc678\ucc98\ub9ac\n", "***\n", "***\n", "- \uad6c\ubb38 \uc5d0\ub7ec (Syntax Error)\n", " - \ubb38\ubc95\uc801 \uc5d0\ub7ec\n", " - \uc774\ud074\ub9bd\uc2a4 \ub4f1\uc758 \ud1b5\ud569\uac1c\ubc1c\ud658\uacbd \ub3c4\uad6c\uc5d0\uc11c\ub294 \uc790\ub3d9\uc73c\ub85c \uc2e4\ud589 \uc804\uc5d0 \uad6c\ubb38 \uc5d0\ub7ec\ub97c \uccb4\ud06c \ud574 \uc90c\n", " - \ud30c\uc774\uc36c\uc740 \uc0c1\ub300\uc801\uc73c\ub85c \uc5b8\uc5b4\uc801 \ubb38\ubc95\uc774 \uac04\ub2e8\ud558\uae30 \ub54c\ubb38\uc5d0 \uad6c\ubb38 \uc790\uccb4\uc758 \uc5d0\ub7ec \ubc1c\uc0dd \ube44\uc728\uc774 \ub0ae\uac70\ub098 \ub2e4\ub978 \ub3c4\uad6c\ub97c \uc0ac\uc6a9\ud558\uc5ec \uc644\ubcbd\ud558\uac8c \uc81c\uac70\ud560 \uc218 \uc788\uc74c\n", "\n", "- \uc608\uc678 (Exception)\n", " - \uad6c\ubb38 \uc5d0\ub7ec\ub294 \uc5c6\uc73c\ub098 \ud504\ub85c\uadf8\ub7a8 \uc2e4\ud589 \uc911 \ub354 \uc774\uc0c1 \uc9c4\ud589 \ud560 \uc218 \uc5c6\ub294 \uc0c1\ud669" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc608\uc678 \ubc1c\uc0dd \uc608\uc81c 1: \uc815\uc758\ub418\uc9c0 \uc54a\uc740 \ubcc0\uc218 \uc0ac\uc6a9\ud558\uae30\n", " - NameError" ] }, { "cell_type": "code", "collapsed": false, "input": [ "4 + spam*3" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'spam' is not defined", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m4\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mspam\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'spam' is not defined" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc608\uc678 \ubc1c\uc0dd \uc608\uc81c 2: 0\uc73c\ub85c \uc22b\uc790 \ub098\ub204\uae30\n", " - ZeroDivisionError" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 10\n", "b = 0 \n", "c = a / b" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "integer division or modulo by zero", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m: integer division or modulo by zero" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- [note] \uc608\uc678\uac00 \ubc1c\uc0dd\ud558\uba74 \ud504\ub85c\uadf8\ub7a8\uc740 \ubc14\ub85c \uc885\ub8cc\ub41c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def division():\n", " for n in range(0, 5):\n", " print 10.0 / n\n", "\n", "division() " ], "language": "python", "metadata": {}, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "float division by zero", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;36m10.0\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mdivision\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mdivision\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdivision\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mn\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0;36m10.0\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mdivision\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mZeroDivisionError\u001b[0m: float division by zero" ] } ], "prompt_number": 48 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc608\uc678 \ubc1c\uc0dd \uc608\uc81c 3: \ubb38\uc790\uc5f4\uacfc \uc22b\uc790 \ub354\ud558\uae30\n", " - TypeError" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'2' + 2" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "cannot concatenate 'str' and 'int' objects", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m'2'\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: cannot concatenate 'str' and 'int' objects" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc608\uc678 \ubc1c\uc0dd \uc608\uc81c 4: \ucc38\uc870 \ubc94\uc704\ub97c \ub118\uc5b4\uc11c \uc778\ub371\uc2a4 \uc0ac\uc6a9\n", " - IndexError" ] }, { "cell_type": "code", "collapsed": false, "input": [ "l = [1, 2]\n", "print l[2]" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ml\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0ml\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc608\uc678 \ubc1c\uc0dd \uc608\uc81c 5: \ub4f1\ub85d\ub418\uc9c0 \uc54a\uc740 \ud0a4\ub85c \uc0ac\uc804 \uac80\uc0c9\n", " - KeyError" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d = {\"a\": 1, \"b\": 2}\n", "print d['c']" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "'c'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0md\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m\"a\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"b\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'c'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 'c'" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc608\uc678 \ubc1c\uc0dd \uc608\uc81c 6: \uc788\uc9c0\ub3c4 \uc54a\uc740 \ud30c\uc77c\uc744 \uc5f4\ub824\uace0 \ud560 \ub54c\n", " - IOError" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = open('aaa.txt')" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "IOError", "evalue": "[Errno 2] No such file or directory: 'aaa.txt'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mIOError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'aaa.txt'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'aaa.txt'" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 01 try/except/else/fianlly \uc808 \uc0ac\uc6a9\ud558\uae30\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc608\uc678\uac00 \ubc1c\uc0dd\ud560 \uc218 \uc788\ub294 \uc0c1\ud669\uc744 \uc608\uc0c1\ud558\uc5ec \uc608\uc678 \ubc1c\uc0dd \uc0c1\ud669\uc744 \uc804\uccb4 \ucf54\ub4dc \ud750\ub984\uc744 \ud568\uaed8 \uc81c\uc5b4\ud560 \uc218 \uc788\ub2e4.\n", "- try/except/else \uc808\n", " - \uad6c\ubb38\n", "> try:
\n", ">     (\uc608\uc678 \ubc1c\uc0dd \uac00\ub2a5\ud55c) \uc77c\ubc18\uc801\uc778 \uc218\ud589\ubb38\ub4e4
\n", "> except Exception:
\n", ">     \uc608\uc678\uac00 \ubc1c\uc0dd\ud558\uc600\uc744 \ub54c \uc218\ud589\ub418\ub294 \ubb38\ub4e4
\n", "> else:
\n", ">     \uc608\uc678\uac00 \ubc1c\uc0dd\ud558\uc9c0 \uc54a\uc558\uc744 \ub54c \uc218\ud589\ub418\ub294 \ubb38\ub4e4
\n", "> finally:
\n", ">     \uc608\uc678 \ubc1c\uc0dd \uc720\ubb34\uc640 \uad00\uacc4\uc5c6\uc774 \ubb34\uc870\uac74 \uc218\ud589\ub418\ub294 \ubb38\ub4e4
" ] }, { "cell_type": "code", "collapsed": false, "input": [ "try:\n", " print 1.0 / 0.0\n", "except ZeroDivisionError:\n", " print 'zero division error!!!'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "zero division error!!!\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc608\uc678 \ucc98\ub9ac\ub97c \ud558\uba74 \ud504\ub85c\uadf8\ub7a8\uc740 \uc885\ub8cc\ub418\uc9c0 \uc54a\ub294\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def division():\n", " for n in range(0, 5):\n", " try:\n", " print 10.0 / n\n", " except ZeroDivisionError, msg:\n", " print msg\n", "\n", "division() " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "float division by zero\n", "10.0\n", "5.0\n", "3.33333333333\n", "2.5\n" ] } ], "prompt_number": 47 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- else: \uad6c\ubb38\uc740 except: \uad6c\ubb38\uc5c6\uc774 \uc0ac\uc6a9 \ubabb\ud55c\ub2e4. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "def division():\n", " for n in range(0, 5):\n", " try:\n", " print 10.0 / n\n", " else:\n", " print \"Success\"\n", "\n", "division() " ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 5)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m5\u001b[0m\n\u001b[0;31m else:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "name = 'aaa.txt'\n", "try:\n", " f = open(name, 'r')\n", "except IOError:\n", " print 'cannot open', name\n", "else:\n", " print name, 'has', len(f.readline()), 'lines'\n", " f.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "cannot open aaa.txt\n" ] } ], "prompt_number": 35 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc0c1\ud669\uc5d0 \ub530\ub77c\uc11c\ub294 \uc5d0\ub7ec\uc640 \ud568\uaed8 \ub530\ub77c\uc624\ub294 \uc815\ubcf4\ub97c \ud568\uaed8 \ubc1b\uc744 \uc218\ub3c4 \uc788\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "try:\n", " spam()\n", "except NameError, msg:\n", " print 'Error - ', msg" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Error - name 'spam' is not defined\n" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "try:\n", " spam()\n", "except NameError as msg:\n", " print 'Error - ', msg" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Error - name 'spam' is not defined\n" ] } ], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- try \uc808 \uc548\uc5d0\uc11c \uac04\uc811\uc801\uc73c\ub85c \ud638\ucd9c\ud55c \ud568\uc218\uc758 \ub0b4\ubd80 \uc608\uc678\ub3c4 \ucc98\ub9ac\ud560 \uc218 \uc788\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def zero_division():\n", " x = 1 / 0\n", "\n", "try:\n", " zero_division()\n", "except ZeroDivisionError, msg:\n", " print 'zero division error!!! - ', msg" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "zero division error!!! - integer division or modulo by zero\n" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "def zero_division():\n", " x = 1 / 0\n", "\n", "try:\n", " zero_division()\n", "except ZeroDivisionError as msg:\n", " print 'zero division error!!! - ', msg" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "zero division error!!! - integer division or modulo by zero\n" ] } ], "prompt_number": 33 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- except \ub4a4\uc5d0 \uc544\ubb34\ub7f0 \uc608\uc678\ub3c4 \uae30\uc220\ud558\uc9c0 \uc54a\uc73c\uba74 \ubaa8\ub4e0 \uc608\uc678\uc5d0 \ub300\ud574 \ucc98\ub9ac\ub41c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "try:\n", " spam()\n", " print 1.0 / 0.0\n", "except:\n", " print 'Error'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Error\n" ] } ], "prompt_number": 31 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc5ec\ub7ec \uc608\uc678\ub4e4 \uac01\uac01\uc5d0 \ub300\ud574 except \uc808\uc744 \ub2e4\uc911\uc73c\ub85c \uc0bd\uc785\ud560 \uc218 \uc788\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "b = 0.0\n", "name = 'aaa.txt'\n", "try:\n", " print 1.0 / b\n", " spam()\n", " f = open(name, 'r')\n", " '2' + 2\n", "except NameError:\n", " print 'NameError !!!'\n", "except ZeroDivisionError:\n", " print 'ZeroDivisionError !!!'\n", "except (TypeError, IOError):\n", " print 'TypeError or IOError !!!'\n", "else:\n", " print 'No Exception !!!'\n", "finally:\n", " print 'Exit !!!'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ZeroDivisionError !!!\n", "Exit !!!\n" ] } ], "prompt_number": 33 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ud30c\uc77c\uc5d0\uc11c \uc22b\uc790\ub97c \uc77d\uc5b4\uc640\uc11c \uc77d\uc740 \uc22b\uc790\ub85c \ub098\ub204\uae30\ub97c \ud558\ub294 \uc608\uc81c\n", " - \uaf3c\uaf3c\ud55c \uc608\uc678 \ucc98\ub9ac \uc608\uc81c" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import os\n", "print os.getcwd()\n", "filename = 't.txt'\n", "\n", "try:\n", " f = open(filename, 'r')\n", "except IOError, msg:\n", " print msg\n", "else:\n", " a = float(f.readline())\n", " try:\n", " answer = 1.0 / a\n", " except ZeroDivisionError, msg:\n", " print msg\n", " else:\n", " print answer\n", " finally:\n", " print \"Finally!!!\"\n", " f.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/Users/yhhan/git/ipython\n", "float division by zero\n", "Finally!!!\n" ] } ], "prompt_number": 43 }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 03 \ub0b4\uc7a5 \uc608\uc678\uc758 \uc885\ub958\n", "***\n", "- \uc608\uc678 \ud074\ub798\uc2a4\uc758 \uacc4\uce35 \uad6c\uc870 ([\ucc38\uace0] https://docs.python.org/2/library/exceptions.html)\n", "- \uac01 \uc608\uc678\uc5d0 \uad00\ud55c \uc124\uba85\n", " - \uad50\uc7ac pp. 473~476 \ucc38\uace0\n", "
BaseException\n",
      " +-- SystemExit\n",
      " +-- KeyboardInterrupt\n",
      " +-- GeneratorExit\n",
      " +-- Exception\n",
      "      +-- StopIteration\n",
      "      +-- StandardError\n",
      "      |    +-- BufferError\n",
      "      |    +-- ArithmeticError\n",
      "      |    |    +-- FloatingPointError\n",
      "      |    |    +-- OverflowError\n",
      "      |    |    +-- ZeroDivisionError\n",
      "      |    +-- AssertionError\n",
      "      |    +-- AttributeError\n",
      "      |    +-- EnvironmentError\n",
      "      |    |    +-- IOError\n",
      "      |    |    +-- OSError\n",
      "      |    |         +-- WindowsError (Windows)\n",
      "      |    |         +-- VMSError (VMS)\n",
      "      |    +-- EOFError\n",
      "      |    +-- ImportError\n",
      "      |    +-- LookupError\n",
      "      |    |    +-- IndexError\n",
      "      |    |    +-- KeyError\n",
      "      |    +-- MemoryError\n",
      "      |    +-- NameError\n",
      "      |    |    +-- UnboundLocalError\n",
      "      |    +-- ReferenceError\n",
      "      |    +-- RuntimeError\n",
      "      |    |    +-- NotImplementedError\n",
      "      |    +-- SyntaxError\n",
      "      |    |    +-- IndentationError\n",
      "      |    |         +-- TabError\n",
      "      |    +-- SystemError\n",
      "      |    +-- TypeError\n",
      "      |    +-- ValueError\n",
      "      |         +-- UnicodeError\n",
      "      |              +-- UnicodeDecodeError\n",
      "      |              +-- UnicodeEncodeError\n",
      "      |              +-- UnicodeTranslateError\n",
      "      +-- Warning\n",
      "           +-- DeprecationWarning\n",
      "           +-- PendingDeprecationWarning\n",
      "           +-- RuntimeWarning\n",
      "           +-- SyntaxWarning\n",
      "           +-- UserWarning\n",
      "           +-- FutureWarning\n",
      "\t   +-- ImportWarning\n",
      "\t   +-- UnicodeWarning\n",
      "\t   +-- BytesWarning\n",
      "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 04 \uac19\uc740 \ubd80\ub958\uc758 \uc608\uc678 \ub2e4 \uc7a1\uc544\ub0b4\uae30\n", "***\n", "- \uc608\uc678 \ud074\ub798\uc2a4\ub4e4\uc740 \uc0c1\uc18d\uc5d0 \uc758\ud55c \uacc4\uce35 \uad00\uacc4\ub97c \uc9c0\ub2c8\uace0 \uc788\uae30 \ub54c\ubb38\uc5d0 \uc774\ub97c \uc774\uc6a9\ud558\uba74 \uc5ec\ub7ec \uc608\uc678\ub4e4\uc744 \ud55c\uaebc\ubc88\uc5d0 \uc7a1\uc744 \uc218 \uc788\ub2e4.\n", "- \uc608\ub97c \ub4e4\uc5b4, ArithmeticError\uc758 \ud558\uc704 \ud074\ub798\uc2a4\ub85c\uc11c FloatingPointError, OverflowError, ZeroDivisionError\uac00 \uc874\uc7ac\ud558\uae30 \ub54c\ubb38\uc5d0 \uc774\ub4e4 \ud558\uc704 \ud074\ub798\uc2a4 \uc608\uc678\uac00 \ubc1c\uc0dd\ud558\uc600\uc744 \uacbd\uc6b0 ArithmeticError\ub85c\uc11c \uc7a1\uc544\ub0bc \uc218 \uc788\ub2e4. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "def dosomething():\n", " a = 1/0\n", "\n", "try:\n", " dosomething()\n", "except ArithmeticError:\n", " print \"ArithmeticException occured\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ArithmeticException occured\n" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc608\uc678\uac00 \uc784\uc758\uc758 except\uc5d0 \uc758\ud574 \uc7a1\ud788\uba74 \ub2e4\ub978 except\uc5d0 \uc758\ud574\uc11c\ub294 \uc7a1\ud788\uc9c0 \uc54a\ub294\ub2e4. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "def dosomething():\n", " a = 1/0\n", "\n", "try:\n", " dosomething()\n", "except ZeroDivisionError: # ZeroDivisionError\ub294 \uc774\uacf3\uc5d0\uc11c \uc7a1\ud78c\ub2e4.\n", " print \"ZeroDivisionError occured\"\n", "except ArithmeticError: # FloatingPointError, OverflowError\ub294 \uc774\uacf3\uc5d0\uc11c \uc7a1\ud78c\ub2e4.\n", " print \"ArithmeticException occured\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ZeroDivisionError occured\n" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "def dosomething():\n", " a = 1/0\n", "\n", "try:\n", " dosomething()\n", "except ArithmeticError:\n", " print \"ArithmeticException occured\"\n", "except ZeroDivisionError: # \uc774\uacf3\uc5d0\uc11c ZeroDivisionError\ub294 \uc7a1\ud788\uc9c0 \uc54a\ub294\ub2e4. ==> \uc798\ubabb\ub41c \ucf54\ub4dc\n", " print \"ZeroDivisionError occured\" " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ArithmeticException occured\n" ] } ], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 05 raise\ub85c \uc608\uc678 \ubc1c\uc0dd\ud558\uae30\n", "***\n", "- \uac01\uc885 \uc608\uc678\ub4e4\uc740 \uc6d0\ub798 exceptions \ubaa8\ub4c8\uc5d0 \ub4f1\ub85d\ub418\uc5b4 \uc788\uc73c\ub098, \uba85\uc2dc\uc801\uc73c\ub85c \uc784\ud3ec\ud2b8\ud558\uc5ec \uc0ac\uc6a9\ud560 \ud544\uc694 \uc5c6\ub2e4.\n", " - \uc790\ub3d9\uc801\uc73c\ub85c \ub0b4\uc7a5 \uc774\ub984 \uacf5\uac04 \uc548\uc5d0 \ubaa8\ub4e0 \uc608\uc678 \ud074\ub798\uc2a4\ub4e4\uc774 \ud3ec\ud568\ub418\uc5b4 \uc788\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for e in dir(__builtin__):\n", " if e.endswith('Error') or e.endswith('Warning'):\n", " print e" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ArithmeticError\n", "AssertionError\n", "AttributeError\n", "BufferError\n", "BytesWarning\n", "DeprecationWarning\n", "EOFError\n", "EnvironmentError\n", "FloatingPointError\n", "FutureWarning\n", "IOError\n", "ImportError\n", "ImportWarning\n", "IndentationError\n", "IndexError\n", "KeyError\n", "LookupError\n", "MemoryError\n", "NameError\n", "NotImplementedError\n", "OSError\n", "OverflowError\n", "PendingDeprecationWarning\n", "ReferenceError\n", "RuntimeError\n", "RuntimeWarning\n", "StandardError\n", "SyntaxError\n", "SyntaxWarning\n", "SystemError\n", "TabError\n", "TypeError\n", "UnboundLocalError\n", "UnicodeDecodeError\n", "UnicodeEncodeError\n", "UnicodeError\n", "UnicodeTranslateError\n", "UnicodeWarning\n", "UserWarning\n", "ValueError\n", "Warning\n", "ZeroDivisionError\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5-1 \ub0b4\uc7a5 \uc608\uc678 \ubc1c\uc0dd\n", "- \ub0b4\uc7a5 \uc601\uc5ed\uc5d0 \uc774\ubbf8 \uc815\uc758\ub418\uc5b4 \uc788\ub294 \uc608\uc678\ub97c \ud2b9\uc815 \uc0c1\ud669 \uc870\uac74\uc5d0\uc11c raise \ud0a4\uc6cc\ub4dc\ub97c \ud1b5\ud574 \ubc1c\uc0dd\uc2dc\ud0ac \uc218 \uc788\ub2e4.\n", "- \uc544\ub798 \uc608\ub294 \uc2dc\ud000\uc2a4 \ud615 \ud074\ub798\uc2a4\ub97c \uc124\uacc4\ud560 \ub54c \uc778\ub371\uc2f1\uc744 \uad6c\ud604\ud558\ub294 \\_\\_getitem\\_\\_ \uba54\uc18c\ub4dc\uc5d0\uc11c \uc778\ub371\uc2a4\uac00 \ubc94\uc704\ub97c \ub118\uc744 \ub54c IndexError\ub97c \ubc1c\uc0dd\uc2dc\ud0a8\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "class SquareSeq:\n", " def __init__(self, n):\n", " self.n = n\n", " def __getitem__(self, k):\n", " if k >= self.n or k < 0 :\n", " raise IndexError # \ucca8\uc790 \ubc94\uc704\ub97c \ubc97\uc5b4\ub098\uba74 IndexError \uc608\uc678\ub97c \ubc1c\uc0dd\uc2dc\ud0b4\n", " return k * k\n", " def __len__(self):\n", " return self.n\n", " \n", "s = SquareSeq(10)\n", "print s[2], s[4]\n", "for x in s: # IndexError\uac00 \ubc1c\uc0dd\ud558\ub294 \uc2dc\uc810\uae4c\uc9c0 \ubc18\ubcf5\ud55c\ub2e4\n", " print x,\n", "print s[20] # \ucca8\uc790 \ubc94\uc704\uac00 \ub118\uc5c8\ub2e4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4 16\n", "0 1 4 9 16 25 36 49 64 81" ] }, { "ename": "IndexError", "evalue": "", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# IndexError\uac00 \ubc1c\uc0dd\ud558\ub294 \uc2dc\uc810\uae4c\uc9c0 \ubc18\ubcf5\ud55c\ub2e4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# \ucca8\uc790 \ubc94\uc704\uac00 \ub118\uc5c8\ub2e4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, k)\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__getitem__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mk\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mn\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mk\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mIndexError\u001b[0m \u001b[0;31m# \ucca8\uc790 \ubc94\uc704\ub97c \ubc97\uc5b4\ub098\uba74 IndexError \uc608\uc678\ub97c \ubc1c\uc0dd\uc2dc\ud0b4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mk\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__len__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mIndexError\u001b[0m: " ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5-2 \uc0ac\uc6a9\uc790 \ud074\ub798\uc2a4 \uc608\uc678 \uc815\uc758 \ubc0f \ubc1c\uc0dd\uc2dc\ud0a4\uae30\n", "- \uc0ac\uc6a9\uc790 \uc815\uc758 \uc608\uc678\ub97c \uad6c\ud604\ud558\ub294 \ud45c\uc900 \ubc29\ubc95\uc740 Exception \ud074\ub798\uc2a4\uc758 \uc11c\ube0c \ud074\ub798\uc2a4\ub85c\uc11c \uad6c\ud604\ud558\ub294 \uac83\n", "- \uc0ac\uc6a9\uc790 \uc815\uc758 \uc608\uc678 \ubc1c\uc0dd \ubc29\ubc95\n", " - \ub0b4\uc7a5 \uc608\uc678 \ubc1c\uc0dd \ubc29\ubc95\uacfc \ub3d9\uc77c\ud558\uac8c raise [\ud074\ub798\uc2a4\uc758 \uc778\uc2a4\ud134\uc2a4] \uc640 \uac19\uc774 \ud574\ub2f9 \uc608\uc678 \ud074\ub798\uc2a4\uc758 \uc778\uc2a4\ud134\uc2a4\ub97c \ub358\uc9c4\ub2e4.\n", "- \uc0ac\uc6a9\uc790 \uc815\uc758 \uc608\uc678\ub97c \uc7a1\ub294 \ubc29\ubc95\n", " - except [\ud074\ub798\uc2a4 \uc774\ub984] \uacfc \uac19\uc774 \ud574\ub2f9 \uc608\uc678 \ud074\ub798\uc2a4 \uc774\ub984\uc744 \uc0ac\uc6a9\ud55c\ub2e4.\n", "- \uc544\ub798 \uc608\uc5d0\uc11c except Big\uc774 \uc7a1\ub294 \uc608\uc678\ub294 Big\uacfc Small \uc774\ub2e4.\n", " - \uc774\uc720: Small\uc740 Big\uc758 \ud558\uc704 \ud074\ub798\uc2a4\uc774\uae30 \ub54c\ubb38" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Big(Exception):\n", " pass\n", "\n", "class Small(Big):\n", " pass\n", "\n", "def dosomething1():\n", " x = Big()\n", " raise x\n", " \n", "def dosomething2():\n", " raise Small()\n", " \n", "for f in (dosomething1, dosomething2):\n", " try:\n", " f()\n", " except Big:\n", " print \"Big Exception = \", Big" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Big Exception = \n", "Big Exception = \n" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5-3 \uc608\uc678\uac12 \uc804\ub2ec\ud558\uae30 \n", "- raise \ud0a4\uc6cc\ub4dc \ub4a4\uc5d0 \uc608\uc678\uc640 \ud568\uaed8, \ucd94\uac00 \uba54\uc2dc\uc9c0\ub97c \ud568\uaed8 \ub358\uc9c8 \uc218 \uc788\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "class MessageClass:\n", " def __init__(self, message, dur):\n", " self.message = message\n", " self.duration = dur\n", " \n", "def f():\n", " raise Exception, 'message'\n", " \n", "try:\n", " f()\n", "except Exception, a:\n", " print a #\uc5d0\ub7ec\ubc1c\uc0dd" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "message\n" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "raise NameError" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mNameError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: " ] } ], "prompt_number": 22 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc5d0\ub7ec \ud074\ub798\uc2a4\uc758 \uc0dd\uc131\uc790 \ud638\ucd9c\uc2dc\uc5d0 \uba54\uc2dc\uc9c0 \uc778\uc790\ub97c \ub123\uc5b4\uc11c \uc804\ub2ec \uac00\ub2a5" ] }, { "cell_type": "code", "collapsed": false, "input": [ "raise NameError('\uc774\ub984 \uc608\uc678')" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "\uc774\ub984 \uc608\uc678", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mNameError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'\uc774\ub984 \uc608\uc678'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: \uc774\ub984 \uc608\uc678" ] } ], "prompt_number": 29 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \uc0dd\uc131\uc790 \uc548\uc5d0 \ub123\uc5b4\uc900 \uc5d0\ub7ec \uba54\uc2dc\uc9c0\ub294 except \ud0a4\uc6cc\ub4dc \uc0ac\uc6a9\uc2dc\uc5d0 \ub450 \ubc88\uc9f8 \uc778\uc790\ub85c \ud574\ub2f9 \uba54\uc2dc\uc9c0\ub97c \ubc1b\uc744 \uc218 \uc788\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 10\n", "b = 0\n", "try:\n", " if b == 0:\n", " raise ArithmeticError('0\uc73c\ub85c \ub098\ub204\uace0 \uc788\uc2b5\ub2c8\ub2e4.')\n", " a / b\n", "except ArithmeticError, v:\n", " print v" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\uc73c\ub85c \ub098\ub204\uace0 \uc788\uc2b5\ub2c8\ub2e4.\n" ] } ], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "a = 10\n", "b = 0\n", "try:\n", " if b == 0:\n", " raise ArithmeticError('0\uc73c\ub85c \ub098\ub204\uace0 \uc788\uc2b5\ub2c8\ub2e4.')\n", " a / b\n", "except ArithmeticError as e:\n", " print e" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\uc73c\ub85c \ub098\ub204\uace0 \uc788\uc2b5\ub2c8\ub2e4.\n" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 06 assert\ubb38\uc73c\ub85c \uc608\uc678 \ubc1c\uc0dd\uc2dc\ud0a4\uae30\n", "***\n", "- assert \ubb38\uc740 \ud504\ub85c\uadf8\ub7a8\uc774 \uc62c\ubc14\ub978 \uc870\uac74\uc73c\ub85c \uc218\ud589\ub418\uace0 \uc788\ub294 \uc9c0\ub97c \ud14c\uc2a4\ud2b8\ud560 \ub54c \uc0ac\uc6a9\ud55c\ub2e4.\n", " - \uc8fc\ub85c \ub514\ubc84\uae45 \uc2dc\uc5d0 \ub9ce\uc774 \ud65c\uc6a9\ub41c\ub2e4.\n", "- \uad6c\ubb38\n", "> assert \uc870\uac74\uc2dd, \uba54\uc2dc\uc9c0\n", "\n", " - \uc870\uac74\uc2dd\uc774 \ub9cc\uc871\ud558\uc9c0 \uc54a\uc73c\uba74 \uba54\uc2dc\uc9c0\uac00 \ucd9c\ub825\ub418\uba70 \ud504\ub85c\uadf8\ub7a8\uc774 \uc885\ub8cc\ub41c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# assert_test.py\n", "a = 30\n", "margin = 2 * 0.2\n", "assert margin > 10, 'not enough margin %s' % margin\n", "print a # assert\uc758 \uc870\uac74\uc2dd\uc774 \uac70\uc9d3\uc774 \ub418\uc5b4 print a\ub294 \uc218\ud589\ub418\uc9c0 \uc54a\ub294\ub2e4." ], "language": "python", "metadata": {}, "outputs": [ { "ename": "AssertionError", "evalue": "not enough margin 0.4", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m30\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mmargin\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m2\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m0.2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mmargin\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'not enough margin %s'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mmargin\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0ma\u001b[0m \u001b[0;31m# assert\uc758 \uc870\uac74\uc2dd\uc774 \uac70\uc9d3\uc774 \ub418\uc5b4 print a\ub294 \uc218\ud589\ub418\uc9c0 \uc54a\ub294\ub2e4.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAssertionError\u001b[0m: not enough margin 0.4" ] } ], "prompt_number": 37 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- assert\uc758 \uc870\uac74\uc774 \ub9cc\uc871\ub418\uc9c0 \uc54a\uc73c\uba74 AssertionError\uac00 \ub358\uc838\uc9c0\uba70 \uc774\ub97c \uc7a1\uc544\uc11c \ucc98\ub9ac\ud560 \uc218\ub3c4 \uc788\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 30\n", "margin = 2 * 0.2\n", "try:\n", " assert margin > 10, 'not enough margin %s' % margin\n", "except AssertionError, msg:\n", " print msg" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "not enough margin 0.4\n" ] } ], "prompt_number": 38 }, { "cell_type": "markdown", "metadata": {}, "source": [ "- \ucf58\uc194\uc5d0\uc11c python \uba85\ub839\uc5b4\ub85c \ud30c\uc774\uc36c \ud30c\uc77c\uc744 \uc218\ud589\ud560 \ub54c -O \uc635\uc158\uc744 \uc8fc\uba74 \ud574\ub2f9 \ud30c\uc77c \uc548\uc5d0 \uc788\ub294 \ubaa8\ub4e0 assert \ubb38\uc740 \ubb34\uc2dc\ub41c\ub2e4.\n", " - -O \uc635\uc158\uc744 \uc8fc\uba74 \\_\\_debug\\_\\_ \uac12\uc774 0\uc774 \ub41c\ub2e4." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 30\n", "margin = 2 * 0.2\n", "if __debug__:\n", " assert margin > 10, 'not enough margin %s' % margin" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "AssertionError", "evalue": "not enough margin 0.4", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mmargin\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m2\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m0.2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m__debug__\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mmargin\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'not enough margin %s'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mmargin\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m: not enough margin 0.4" ] } ], "prompt_number": 19 } ], "metadata": {} } ] }