{ "metadata": { "name": "", "signature": "sha256:636fe844c88ef46d09064f199c6b3a4c62e262611a41cc6f126f08459b94982e" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 17: Exception Handling

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.1, Page Number: 397

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"start\"\n", "\n", "try: #start a try block\n", " print \"Inside try block\"\n", " raise Exception(99) #raise an error\n", " print \"This will not execute\"\n", "except Exception,i: #catch an error\n", " print \"Caught an exception -- value is:\",\n", " print i\n", "\n", "print \"end\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "start\n", "Inside try block\n", "Caught an exception -- value is: 99\n", "end\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.2, Page Number: 399

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "import sys\n", "\n", "\n", "\n", "def main():\n", " print \"start\"\n", " try: #start a try block\n", " print \"Inside try block\"\n", " raise Exception(99) #raise an error\n", " print \"This will not execute\"\n", " except Exception,i: #catch an error\n", " if isinstance(i,float):\n", " print \"Caught an exception -- value is:\",\n", " print i\n", " else:\n", " print \"Abnormal program termination\"\n", " return\n", " print \"end\"\n", "\n", " \n", "main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "start\n", "Inside try block\n", "Abnormal program termination\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.3, Page Number: 400

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def Xtest(test):\n", " print \"Inside Xtest, test is: \",test\n", " if(test):\n", " raise Exception(test)\n", " \n", "print \"start\"\n", "\n", "try: #start a try block\n", " print \"Inside try block\"\n", " Xtest(0)\n", " Xtest(1)\n", " Xtest(2)\n", "except Exception,i: #catch an error\n", " print \"Caught an exception -- value is:\",\n", " print i\n", "\n", "print \"end\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "start\n", "Inside try block\n", "Inside Xtest, test is: 0\n", "Inside Xtest, test is: 1\n", "Caught an exception -- value is: 1\n", "end\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.4, Page Number: 401

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def Xhandler(test):\n", " try:\n", " if(test):\n", " raise Exception(test)\n", " except Exception,i:\n", " print \"Caught One! Ex #:\",i\n", " \n", "print \"start\"\n", "\n", "Xhandler(1)\n", "Xhandler(2)\n", "Xhandler(0)\n", "Xhandler(3)\n", "\n", "print \"end\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "start\n", "Caught One! Ex #: 1\n", "Caught One! Ex #: 2\n", "Caught One! Ex #: 3\n", "end\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.5, Page Number: 401

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class MyException:\n", " def __init__(self,s):\n", " self.str_what=s\n", " \n", "#Variable declaration\n", "a=None \n", "b=None\n", "\n", "try:\n", " print \"Enter numerator and denominator:\"\n", " #User-input\n", " a=10 \n", " b=0\n", " if not(b):\n", " raise MyException(\"Cannot divide by zero!\")\n", " else:\n", " print \"Quotient is\",a/b\n", "except MyException as e: #catch an error\n", " print e.str_what\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter numerator and denominator:\n", "Cannot divide by zero!\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.6, Page Number: 403

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class MyException:\n", " def __init__(self,s):\n", " self.x=s\n", " \n", "def Xhandler(test):\n", " try:\n", " if(test):\n", " raise MyException(test)\n", " else:\n", " raise MyException(\"Value is zero\")\n", " except MyException as i:\n", " if isinstance(i.x,int):\n", " print \"Caught One! Ex #:\",i.x\n", " else:\n", " print \"Caught a string:\",\n", " print i.x\n", " \n", "print \"start\"\n", "\n", "Xhandler(1)\n", "Xhandler(2)\n", "Xhandler(0)\n", "Xhandler(3)\n", "\n", "print \"end\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "start\n", "Caught One! Ex #: 1\n", "Caught One! Ex #: 2\n", "Caught a string: Value is zero\n", "Caught One! Ex #: 3\n", "end\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.7, Page Number: 404

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class B:\n", " pass\n", "\n", "class D(B):\n", " pass\n", "\n", "derived=D()\n", "\n", "try:\n", " raise B()\n", "except B as b:\n", " print \"Caught a base class.\"\n", "except D as d:\n", " print \"This wont execute.\"\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Caught a base class.\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.8, Page Number: 405

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def Xhandler(test):\n", " try:\n", " if test==0:\n", " raise Exception(test) #throw int\n", " if test==1:\n", " raise Exception('a') #throw char\n", " if test==2:\n", " raise Exception(123.23) #throw double\n", " except: #Catches all exceptions\n", " print \"Caught One!\"\n", "\n", "print \"start\"\n", "\n", "Xhandler(0)\n", "Xhandler(1)\n", "Xhandler(2)\n", "\n", "print \"end\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "start\n", "Caught One!\n", "Caught One!\n", "Caught One!\n", "end\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.9, Page Number: 405

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class MyException:\n", " def __init__(self,s):\n", " self.x=s\n", " \n", "def Xhandler(test):\n", " try:\n", " if test==0:\n", " raise MyException(test) #throw int\n", " if test==1:\n", " raise MyException('a') #throw char\n", " if test==2:\n", " raise MyException(123.23) #throw double\n", " except MyException as i:\n", " if isinstance(i.x,int): #catch an int exception\n", " print \"Caught\",i.x \n", " else: #catch all other exceptions\n", " print \"Caught One!\"\n", " \n", "\n", "print \"start\"\n", "\n", "Xhandler(0)\n", "Xhandler(1)\n", "Xhandler(2)\n", "\n", "print \"end\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "start\n", "Caught 0\n", "Caught One!\n", "Caught One!\n", "end\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.10, Page Number: 407

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class MyException:\n", " def __init__(self,s):\n", " self.x=s\n", " \n", "#This function can only throw ints, chars and doubles\n", "def Xhandler(test):\n", " if test==0:\n", " raise MyException(test) #throw int\n", " if test==1:\n", " raise MyException('a') #throw char\n", " if test==2:\n", " raise MyException(123.23) #throw double\n", " \n", "\n", "print \"start\"\n", "try:\n", " Xhandler(0)\n", "except MyException as i:\n", " if isinstance(i.x,int): #catch an int exception\n", " print \"Caught int\" \n", " elif isinstance(i.x,str): #catch a char exception\n", " print \"Caught char\"\n", " elif isinstance(i.x,float): #catch a float exception\n", " print \"Caught double\"\n", "\n", "print \"end\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "start\n", "Caught int\n", "end\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.11, Page Number: 408

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def Xhandler():\n", " try:\n", " raise Exception(\"hello\") #throw a char *\n", " except Exception,c: #catch a char *\n", " print \"Caugh char * inside Xhandler\"\n", " raise #rethrow char * out of function\n", " \n", "print \"start\"\n", "try:\n", " Xhandler()\n", "except Exception,c:\n", " print \"Caught char * inside main\"\n", " \n", "print \"end\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "start\n", "Caugh char * inside Xhandler\n", "Caught char * inside main\n", "end\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.12, Page Number: 410

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "#Variable declaration\n", "p=[]\n", "\n", "try:\n", " for i in range(32):\n", " p.append(c_int())\n", "except MemoryError,m:\n", " print \"Allocation failure.\"\n", " \n", "for i in range(32):\n", " p[i]=i\n", " \n", "for i in range(32):\n", " print p[i],\n", " \n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.13, Page Number: 410

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from ctypes import *\n", "\n", "#Variable declaration\n", "p=[]\n", "\n", "\n", "for i in range(32):\n", " p.append(c_int()) \n", "if not(p):\n", " print \"Allocation failure.\"\n", " \n", "for i in range(32):\n", " p[i]=i\n", " \n", "for i in range(32):\n", " print p[i],\n", " \n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31\n" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 17.13, Page Number: 412

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class three_d:\n", " def __init__(self,i=0,j=0,k=0): #3=D coordinates\n", " if(i==0 and j==0 and k==0):\n", " self.x=self.y=self.z=0\n", " print \"Constructing 0, 0, 0\"\n", " else:\n", " self.x=i\n", " self.y=j\n", " self.z=k\n", " print \"Constructing\",i,\",\",j,\",\",k\n", " def __del__(self):\n", " print \"Destructing\"\n", " #new overloaded relative to three_d.\n", " def __new__(typ, *args, **kwargs):\n", " obj = object.__new__(typ, *args, **kwargs)\n", " return obj\n", " def show(self):\n", " print self.x,\",\",\n", " print self.y,\",\",\n", " print self.z\n", " \n", "p1=[]*3\n", "p2=[]\n", " \n", "try:\n", " print \"Allocating array of three_d objects.\"\n", " for i in range(3): #allocate array\n", " p1.append(three_d())\n", " print \"Allocating three_d object.\"\n", " p2.append(three_d(5,6,7)) #allocate object\n", "except MemoryError:\n", " print \"Allocation error\"\n", " \n", "p1[2].show()\n", "p2[0].show()\n", "\n", "\n", "for i in xrange(2,-1,-1):\n", " del p1[i] #delete array\n", "print \"Deleting array of thee_d objects.\"\n", "\n", "del p2[0] #delete object\n", "print \"Deleting three_d object.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Allocating array of three_d objects.\n", "Constructing 0, 0, 0\n", "Constructing 0, 0, 0\n", "Constructing 0, 0, 0\n", "Allocating three_d object.\n", "Constructing 5 , 6 , 7\n", "0 , 0 , 0\n", "5 , 6 , 7\n", "Destructing\n", "Destructing\n", "Destructing\n", "Deleting array of thee_d objects.\n", "Destructing\n", "Deleting three_d object.\n" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }