{ "metadata": { "name": "", "signature": "sha256:5b64344dd4b10e210167de1e2d127d9539461b4485363c51a2c3ddba9df7db38" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 13: Managing Errors & Exceptions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.1, page no. 231" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\"\"\"\n", "there is no need of semicolon. We will show error for something else\n", "\"\"\"\n", "\n", "print \"Hello Python...\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello Python...\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.2, page no. 232" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "a = 10\n", "b = 5\n", "c = 5\n", "x = a/(b-c) #It will show ZeroDivisionError\n", "print \"x = \", x\n", "y = a/(b+c)\n", "print \"y = \", y" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "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[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"x = \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13.3 page no : 235" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "a = 10\n", "b = 5\n", "c = 5\n", "try:\n", " x = a/(b-c)\n", "except Exception,e :\n", " print \"Division by zero\"\n", "y = a/(b+c)\n", "print \"y = \",y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Division by zero\n", "y = 1\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13.4 page no : 236" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys\n", "invalid = 0\n", "count = 0\n", "for i in sys.argv:\n", " try:\n", " a = int(i)\n", " except:\n", " invalid += 1\n", " print \"Invalid number\" , i\n", " continue\n", " count += 1\n", "\n", "print \"Valid Numbers : \" ,count\n", "print \"Invalid numbers \",invalid\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " Invalid number -c\n", "Invalid number -f\n", "Invalid number /tmp/tmp1tOqar/profile_default/security/kernel-082ebfe6-5650-4ba4-8bcf-cd17bc99af7a.json\n", "Invalid number --IPKernelApp.parent_appname='ipython-notebook'\n", "Invalid number --profile-dir\n", "Invalid number /tmp/tmp1tOqar/profile_default\n", "Invalid number --parent=1\n", "Valid Numbers : 0\n", "Invalid numbers 7\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 13.5, page no. 239" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "a = [5,10]\n", "b = 5\n", "try:\n", " x = a[2]/b - a[1]\n", "except AssertionError:\n", " print \"AssertionError:\"\n", "except IndexError:\n", " print \"Array Index Error\"\n", "except Exception:\n", " print \"Any Error\"\n", "y = a[1]/a[0]\n", "print \"y = \" , y " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Array Index Error\n", "y = 2\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13.6 page no : 239" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "x = 5\n", "y = 1000\n", "\n", "class MyException(Exception):\n", " def __init__(self, message):\n", " Exception.__init__(self, message)\n", " def error(self):\n", " print self.message\n", "\n", "try:\n", " z = x/y\n", " if(z<0.01):\n", " raise MyException(\"Number is too small\")\n", "except MyException, error:\n", " print \"Caught MyException\"\n", " print error.message\n", "finally:\n", " print \"I am always here\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Caught MyException\n", "Number is too small\n", "I am always here\n" ] } ], "prompt_number": 8 } ], "metadata": {} } ] }