summaryrefslogtreecommitdiff
path: root/Programming_With_Java_A_Primer/chapter13.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Programming_With_Java_A_Primer/chapter13.ipynb')
-rw-r--r--Programming_With_Java_A_Primer/chapter13.ipynb267
1 files changed, 267 insertions, 0 deletions
diff --git a/Programming_With_Java_A_Primer/chapter13.ipynb b/Programming_With_Java_A_Primer/chapter13.ipynb
new file mode 100644
index 00000000..7c07d269
--- /dev/null
+++ b/Programming_With_Java_A_Primer/chapter13.ipynb
@@ -0,0 +1,267 @@
+{
+ "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": [
+ {
+ "ename": "SyntaxError",
+ "evalue": "EOL while scanning string literal (<ipython-input-1-c13cce4dd116>, line 7)",
+ "output_type": "pyerr",
+ "traceback": [
+ "\u001b[1;36m File \u001b[1;32m\"<ipython-input-1-c13cce4dd116>\"\u001b[1;36m, line \u001b[1;32m7\u001b[0m\n\u001b[1;33m print \"Hello Python...\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m EOL while scanning string literal\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "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)\n",
+ "print \"x = \", x\n",
+ "y = a/(b+c)\n",
+ "print \"y = \", y"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "ename": "ZeroDivisionError",
+ "evalue": "integer division or modulo by zero",
+ "output_type": "pyerr",
+ "traceback": [
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
+ "\u001b[1;32m<ipython-input-2-d52b7922541b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"x = \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
+ "\u001b[1;31mZeroDivisionError\u001b[0m: integer division or modulo by zero"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "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": {}
+ }
+ ]
+} \ No newline at end of file