summaryrefslogtreecommitdiff
path: root/Programming_With_Java_A_Primer
diff options
context:
space:
mode:
Diffstat (limited to 'Programming_With_Java_A_Primer')
-rwxr-xr-xProgramming_With_Java_A_Primer/.ipynb_checkpoints/chapter13-checkpoint.ipynb266
-rwxr-xr-xProgramming_With_Java_A_Primer/chapter13.ipynb25
-rwxr-xr-xProgramming_With_Java_A_Primer/chapter17.ipynb10
3 files changed, 283 insertions, 18 deletions
diff --git a/Programming_With_Java_A_Primer/.ipynb_checkpoints/chapter13-checkpoint.ipynb b/Programming_With_Java_A_Primer/.ipynb_checkpoints/chapter13-checkpoint.ipynb
new file mode 100755
index 00000000..28830641
--- /dev/null
+++ b/Programming_With_Java_A_Primer/.ipynb_checkpoints/chapter13-checkpoint.ipynb
@@ -0,0 +1,266 @@
+{
+ "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<ipython-input-5-9289236a4a07>\u001b[0m in \u001b[0;36m<module>\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": {}
+ }
+ ]
+} \ No newline at end of file
diff --git a/Programming_With_Java_A_Primer/chapter13.ipynb b/Programming_With_Java_A_Primer/chapter13.ipynb
index 7c07d269..28830641 100755
--- a/Programming_With_Java_A_Primer/chapter13.ipynb
+++ b/Programming_With_Java_A_Primer/chapter13.ipynb
@@ -33,21 +33,20 @@
"there is no need of semicolon. We will show error for something else\n",
"\"\"\"\n",
"\n",
- "print \"Hello Python..."
+ "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"
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello Python...\n"
]
}
],
- "prompt_number": 1
+ "prompt_number": 2
},
{
"cell_type": "heading",
@@ -65,7 +64,7 @@
"a = 10\n",
"b = 5\n",
"c = 5\n",
- "x = a/(b-c)\n",
+ "x = a/(b-c) #It will show ZeroDivisionError\n",
"print \"x = \", x\n",
"y = a/(b+c)\n",
"print \"y = \", y"
@@ -75,16 +74,16 @@
"outputs": [
{
"ename": "ZeroDivisionError",
- "evalue": "integer division or modulo by zero",
+ "evalue": "division 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"
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
+ "\u001b[0;32m<ipython-input-5-9289236a4a07>\u001b[0m in \u001b[0;36m<module>\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": 2
+ "prompt_number": 5
},
{
"cell_type": "heading",
diff --git a/Programming_With_Java_A_Primer/chapter17.ipynb b/Programming_With_Java_A_Primer/chapter17.ipynb
index c8344546..75e227d6 100755
--- a/Programming_With_Java_A_Primer/chapter17.ipynb
+++ b/Programming_With_Java_A_Primer/chapter17.ipynb
@@ -45,14 +45,14 @@
"evalue": "The value b cannot be zero",
"output_type": "pyerr",
"traceback": [
- "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)",
- "\u001b[1;32m<ipython-input-3-39ef7600cfd3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"The result is: \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[0mdiv\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mDivision\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0mdiv\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0massertcheck\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[1;32m<ipython-input-3-39ef7600cfd3>\u001b[0m in \u001b[0;36massertcheck\u001b[1;34m(self, a, b)\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mclass\u001b[0m \u001b[0mDivision\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0massertcheck\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[1;32massert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m!=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"The value b cannot be zero\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[0mc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"The result is: \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
- "\u001b[1;31mAssertionError\u001b[0m: The value b cannot be zero"
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
+ "\u001b[0;32m<ipython-input-1-426e5021b6ba>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"The result is: \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mdiv\u001b[0m \u001b[0;34m=\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;32m----> 8\u001b[0;31m \u001b[0mdiv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0massertcheck\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\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[0m",
+ "\u001b[0;32m<ipython-input-1-426e5021b6ba>\u001b[0m in \u001b[0;36massertcheck\u001b[0;34m(self, a, b)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mclass\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 3\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0massertcheck\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\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[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32massert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m!=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"The value b cannot be zero\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \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[1;32m 6\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"The result is: \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+ "\u001b[0;31mAssertionError\u001b[0m: The value b cannot be zero"
]
}
],
- "prompt_number": 3
+ "prompt_number": 1
}
],
"metadata": {}