From 605cc9ca1b4b91e60737e48e1c917b09f430f4a4 Mon Sep 17 00:00:00 2001 From: dhavalsays Date: Thu, 6 Feb 2020 22:13:13 -0500 Subject: [PATCH] update exception handling notebook --- 3_exception_handling.ipynb | 1314 ++++++++++++++++++------------------ 1 file changed, 663 insertions(+), 651 deletions(-) diff --git a/3_exception_handling.ipynb b/3_exception_handling.ipynb index 266f4a0..f41cc28 100644 --- a/3_exception_handling.ipynb +++ b/3_exception_handling.ipynb @@ -1,659 +1,671 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "m9uJyhSjcOI-" + }, + "source": [ + "#**Exception Handlilng In Python**" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "tsCcaY2NcKPE" + }, + "outputs": [], + "source": [ + "def divide_numbers(a,b):\n", + " return a/b" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { "colab": { - "name": "exception_handling.ipynb", - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "colab_type": "code", + "id": "5MoaYoy0cSTl", + "outputId": "2043e88d-ed00-4100-9112-f1966c75a216" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5.0" + ] + }, + "execution_count": 2, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" } + ], + "source": [ + "divide_numbers(10,2)" + ] }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "m9uJyhSjcOI-", - "colab_type": "text" - }, - "source": [ - "#**Exception Handlilng In Python**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "tsCcaY2NcKPE", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def divide_numbers(a,b):\n", - " return a/b" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "5MoaYoy0cSTl", - "colab_type": "code", - "outputId": "2043e88d-ed00-4100-9112-f1966c75a216", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "divide_numbers(10,2)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "5.0" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 2 - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "pUgT4pjvcTlW", - "colab_type": "code", - "outputId": "967877bc-2d54-4651-b743-1b50f7d8b8b7", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 233 - } - }, - "source": [ - "divide_numbers(10,0)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "error", - "ename": "ZeroDivisionError", - "evalue": "ignored", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\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[0;32m----> 1\u001b[0;31m \u001b[0mdivide_numbers\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\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\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m\u001b[0m in \u001b[0;36mdivide_numbers\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdivide_numbers\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[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\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[0m\n\u001b[0m", - "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" - ] - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "I5m5FdAqcYC2", - "colab_type": "text" - }, - "source": [ - "**Now let's handle the exception in our function**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "bd9D7e_TcXE2", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def divide_numbers(a,b):\n", - " try:\n", - " d = a/b\n", - " except ZeroDivisionError as e:\n", - " print(e)\n", - " d = None" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "kwaXnur5cdO2", - "colab_type": "code", - "outputId": "50770cfc-e74e-4cbe-c10d-8cbfec492f2f", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "divide_numbers(10,0)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "stream", - "text": [ - "division by zero\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "z2i7SZqxchWJ", - "colab_type": "text" - }, - "source": [ - "### **Handling multiple exceptions**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "F5qw7hvZcfVO", - "colab_type": "code", - "outputId": "bf9bcf8c-1ae2-44cc-d3e7-a530d5a9bf03", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 353 - } - }, - "source": [ - "x=input(\"Enter number 1:\")\n", - "y=input(\"Enter number 2:\")\n", - "divide_numbers(x,y)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Enter number 1:10\n", - "Enter number 2:5\n" - ], - "name": "stdout" - }, - { - "output_type": "error", - "ename": "TypeError", - "evalue": "ignored", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\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[1;32m 1\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter number 1:\"\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[0my\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter number 2:\"\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[0mdivide_numbers\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0my\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;36mdivide_numbers\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdivide_numbers\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[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mtry\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[0md\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[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mZeroDivisionError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\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[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for /: 'str' and 'str'" - ] - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "D3ErIlgdcoDf", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def divide_numbers(a,b):\n", - " try:\n", - " d = a/b\n", - " except TypeError as e:\n", - " print(e)\n", - " d = None\n", - " except ZeroDivisionError as e:\n", - " print(e)\n", - " d = None" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "bxlY5DsAco2_", - "colab_type": "code", - "outputId": "288631e1-fe9a-4b00-dc79-60c23c87da11", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 - } - }, - "source": [ - "x=input(\"Enter number 1:\")\n", - "y=input(\"Enter number 2:\")\n", - "divide_numbers(x,y)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Enter number 1:10\n", - "Enter number 2:5\n", - "unsupported operand type(s) for /: 'str' and 'str'\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "NN_9JH7ScuHO", - "colab_type": "text" - }, - "source": [ - "### **Let's make our function fully functional**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "Au5dYzf0cvJ2", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def divide_numbers(a,b):\n", - " try:\n", - " d = float(a)/float(b)\n", - " except TypeError as e:\n", - " print(e)\n", - " d = None\n", - " except ZeroDivisionError as e:\n", - " print(e)\n", - " d = None\n", - " return d" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "_V9nZB5ycxEW", - "colab_type": "code", - "outputId": "e438eaf4-c5c0-40da-f47e-cb52d470cebb", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 - } - }, - "source": [ - "x=input(\"Enter number 1:\")\n", - "y=input(\"Enter number 2:\")\n", - "print(divide_numbers(x,y))" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Enter number 1:10\n", - "Enter number 2:4\n", - "2.5\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "OvJFYYe6c0av", - "colab_type": "text" - }, - "source": [ - "### **Catching all exceptions**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "jpvT7UiZczse", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def divide_numbers(a,b):\n", - " try:\n", - " d = float(a)/float(b)\n", - " except Exception as e:\n", - " print(\"Exception occured!\")\n", - " d = None\n", - " return d" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "2-DUM16ic4iG", - "colab_type": "code", - "outputId": "47f719a4-07a6-4e60-d41e-d5276aaa62b1", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "divide_numbers(10,0)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Exception occured!\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "Uq41czmlc51O", - "colab_type": "code", - "outputId": "7a29da69-ff2f-4438-f20f-b71f7ba2b42f", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "divide_numbers('abc',0)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Exception occured!\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "y3UfZiYpc8xR", - "colab_type": "text" - }, - "source": [ - "### **Raising exception**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "IKPJB2tIc-d_", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def get_grade(score):\n", - " if score>70:\n", - " return \"A\"\n", - " if score>=50 and score<=70:\n", - " return \"B\"\n", - " if score<50:\n", - " return \"C\"" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "SQP_y6c8dATn", - "colab_type": "code", - "outputId": "8e0a0d56-ddbb-498e-ece6-3e75d789c9d2", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "get_grade(90)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "'A'" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 15 - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "7-0DN04gdBqm", - "colab_type": "code", - "outputId": "5d54497a-09ed-4d74-e3f9-40d41e32d29b", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "get_grade(23)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "'C'" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 16 - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "vupyGdL1dDfn", - "colab_type": "code", - "outputId": "0a1986c0-b063-4612-c546-c53ee6dfaa24", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "get_grade(200)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "'A'" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 17 - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "aHBCYAGZdEZx", - "colab_type": "text" - }, - "source": [ - "**Here the max test score is 100. So passing 200 as a parameter to this function is clearly invalid. The function should be doing the validation that score should be between 0 to 100 and raising exception if it is out side this range.**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "nRcuv2mTdGPh", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def get_grade(score):\n", - " if score<0 or score>100:\n", - " raise ValueError(\"Invalid score\")\n", - "\n", - " if score>70:\n", - " return \"A\"\n", - " if score>=50 and score<=70:\n", - " return \"B\"\n", - " if score<50:\n", - " return \"C\"" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "3XVL5NAydINa", - "colab_type": "code", - "outputId": "06beddb3-488c-4ccb-aacd-b75be902c322", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 284 - } - }, - "source": [ - "get_grade(200)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "error", - "ename": "ValueError", - "evalue": "ignored", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mValueError\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[0mget_grade\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m200\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;36mget_grade\u001b[0;34m(score)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mget_grade\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mscore\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;32mif\u001b[0m \u001b[0mscore\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0;36m0\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mscore\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;36m100\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;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Invalid score\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\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[0;32mif\u001b[0m \u001b[0mscore\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;36m70\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mValueError\u001b[0m: Invalid score" - ] - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "5C6ZjqordKse", - "colab_type": "code", - "colab": {} - }, - "source": [ - "" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "nHDUEgzgdMR2", - "colab_type": "text" - }, - "source": [ - "## **Excercise**" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Xwgoad5xdNAv", - "colab_type": "text" - }, - "source": [ - "**1. Write a function that takes file name as an input and returns the content of that file as an output. You will call this function for a file called idontexist.txt. This file doesn't exist, see what happens when you try to open this file. Now in your function handle file note opening exception and return a message to user saying that this file doesn't exist!**" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "KU3Y60iFdOv4", - "colab_type": "text" - }, - "source": [ - "\n", - "```\n", - "def get_file_content(file_name):\n", - " # your code goes here\n", - "\n", - "get_file_content(\"idontexist.txt\")\n", - "```\n" + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 233 + }, + "colab_type": "code", + "id": "pUgT4pjvcTlW", + "outputId": "967877bc-2d54-4651-b743-1b50f7d8b8b7" + }, + "outputs": [ + { + "ename": "ZeroDivisionError", + "evalue": "ignored", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\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[0;32m----> 1\u001b[0;31m \u001b[0mdivide_numbers\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\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\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mdivide_numbers\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdivide_numbers\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[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\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[0m\n\u001b[0m", + "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" + ] + } + ], + "source": [ + "divide_numbers(10,0)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "I5m5FdAqcYC2" + }, + "source": [ + "**Now let's handle the exception in our function**" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "bd9D7e_TcXE2" + }, + "outputs": [], + "source": [ + "def divide_numbers(a,b):\n", + " try:\n", + " d = a/b\n", + " except ZeroDivisionError as e:\n", + " print(e)\n", + " d = None\n", + " return d" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "colab_type": "code", + "id": "kwaXnur5cdO2", + "outputId": "50770cfc-e74e-4cbe-c10d-8cbfec492f2f" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "division by zero\n" + ] + } + ], + "source": [ + "divide_numbers(10,0)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "z2i7SZqxchWJ" + }, + "source": [ + "### **Handling multiple exceptions**" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 353 + }, + "colab_type": "code", + "id": "F5qw7hvZcfVO", + "outputId": "bf9bcf8c-1ae2-44cc-d3e7-a530d5a9bf03" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter number 1:10\n", + "Enter number 2:5\n" + ] + }, + { + "ename": "TypeError", + "evalue": "ignored", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\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[1;32m 1\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter number 1:\"\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[0my\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter number 2:\"\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[0mdivide_numbers\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0my\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;36mdivide_numbers\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdivide_numbers\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[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mtry\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[0md\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[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mZeroDivisionError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\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[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for /: 'str' and 'str'" + ] + } + ], + "source": [ + "x=input(\"Enter number 1:\")\n", + "y=input(\"Enter number 2:\")\n", + "divide_numbers(x,y)" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "D3ErIlgdcoDf" + }, + "outputs": [], + "source": [ + "def divide_numbers(a,b):\n", + " try:\n", + " d = a/b\n", + " except TypeError as e:\n", + " print(e)\n", + " d = None\n", + " except ZeroDivisionError as e:\n", + " print(e)\n", + " d = None" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "colab_type": "code", + "id": "bxlY5DsAco2_", + "outputId": "288631e1-fe9a-4b00-dc79-60c23c87da11" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter number 1:10\n", + "Enter number 2:5\n", + "unsupported operand type(s) for /: 'str' and 'str'\n" + ] + } + ], + "source": [ + "x=input(\"Enter number 1:\")\n", + "y=input(\"Enter number 2:\")\n", + "divide_numbers(x,y)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "NN_9JH7ScuHO" + }, + "source": [ + "### **Let's make our function fully functional**" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "Au5dYzf0cvJ2" + }, + "outputs": [], + "source": [ + "def divide_numbers(a,b):\n", + " try:\n", + " d = float(a)/float(b)\n", + " except TypeError as e:\n", + " print(e)\n", + " d = None\n", + " except ZeroDivisionError as e:\n", + " print(e)\n", + " d = None\n", + " return d" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "colab_type": "code", + "id": "_V9nZB5ycxEW", + "outputId": "e438eaf4-c5c0-40da-f47e-cb52d470cebb" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter number 1:10\n", + "Enter number 2:4\n", + "2.5\n" + ] + } + ], + "source": [ + "x=input(\"Enter number 1:\")\n", + "y=input(\"Enter number 2:\")\n", + "print(divide_numbers(x,y))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "OvJFYYe6c0av" + }, + "source": [ + "### **Catching all exceptions**" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "jpvT7UiZczse" + }, + "outputs": [], + "source": [ + "def divide_numbers(a,b):\n", + " try:\n", + " d = float(a)/float(b)\n", + " except Exception as e:\n", + " print(\"Exception occured!\")\n", + " d = None\n", + " return d" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "colab_type": "code", + "id": "2-DUM16ic4iG", + "outputId": "47f719a4-07a6-4e60-d41e-d5276aaa62b1" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Exception occured!\n" + ] + } + ], + "source": [ + "divide_numbers(10,0)" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "colab_type": "code", + "id": "Uq41czmlc51O", + "outputId": "7a29da69-ff2f-4438-f20f-b71f7ba2b42f" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Exception occured!\n" + ] + } + ], + "source": [ + "divide_numbers('abc',0)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "y3UfZiYpc8xR" + }, + "source": [ + "### **Raising exception**" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "IKPJB2tIc-d_" + }, + "outputs": [], + "source": [ + "def get_grade(score):\n", + " if score>70:\n", + " return \"A\"\n", + " if score>=50 and score<=70:\n", + " return \"B\"\n", + " if score<50:\n", + " return \"C\"" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "colab_type": "code", + "id": "SQP_y6c8dATn", + "outputId": "8e0a0d56-ddbb-498e-ece6-3e75d789c9d2" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'A'" ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "-wgcd9fddQ15", - "colab_type": "text" - }, - "source": [ - "**2. Write a function to calculate area of a triangle. This function takes base and height as parameters and computes triangle area by using formula,**\n", - "\n", - "```\n", - "area = (base*height)/2\n", - "```\n", - "**Call this function using string arguments. See what happens. Now change your function such that it throws appropriate exception when data type of base and height is not a number. It should also throw exception when either base or height is negative**" + }, + "execution_count": 15, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "get_grade(90)" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "colab_type": "code", + "id": "7-0DN04gdBqm", + "outputId": "5d54497a-09ed-4d74-e3f9-40d41e32d29b" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'C'" ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "RGtRqReOdSjO", - "colab_type": "text" - }, - "source": [ - "\n", - "\n", - "```\n", - "def triangle_area(base,height):\n", - " # your code goes here\n", - "\n", - "triangle_area(20,10) # This works ok\n", - "triangle_area(\"abc\",10) # This should throw exception saying base value is invalid\n", - "triangle_area(23,\"\") # This should throw exception saying value of height is invalid\n", - "triangle_area(-5,10) # This should throw exception saying base value can't be negative\n", - "triangle_area(8,-3) # Throws exception saying height can't be negative\n", - "```" + }, + "execution_count": 16, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "get_grade(23)" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "colab_type": "code", + "id": "vupyGdL1dDfn", + "outputId": "0a1986c0-b063-4612-c546-c53ee6dfaa24" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'A'" ] + }, + "execution_count": 17, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "get_grade(200)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "aHBCYAGZdEZx" + }, + "source": [ + "**Here the max test score is 100. So passing 200 as a parameter to this function is clearly invalid. The function should be doing the validation that score should be between 0 to 100 and raising exception if it is out side this range.**" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "nRcuv2mTdGPh" + }, + "outputs": [], + "source": [ + "def get_grade(score):\n", + " if score<0 or score>100:\n", + " raise ValueError(\"Invalid score\")\n", + "\n", + " if score>70:\n", + " return \"A\"\n", + " if score>=50 and score<=70:\n", + " return \"B\"\n", + " if score<50:\n", + " return \"C\"" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 284 + }, + "colab_type": "code", + "id": "3XVL5NAydINa", + "outputId": "06beddb3-488c-4ccb-aacd-b75be902c322" + }, + "outputs": [ + { + "ename": "ValueError", + "evalue": "ignored", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\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[0mget_grade\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m200\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;36mget_grade\u001b[0;34m(score)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mget_grade\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mscore\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;32mif\u001b[0m \u001b[0mscore\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0;36m0\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mscore\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;36m100\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;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Invalid score\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\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[0;32mif\u001b[0m \u001b[0mscore\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;36m70\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mValueError\u001b[0m: Invalid score" + ] } - ] -} \ No newline at end of file + ], + "source": [ + "get_grade(200)" + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "5C6ZjqordKse" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "nHDUEgzgdMR2" + }, + "source": [ + "## **Excercise**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "Xwgoad5xdNAv" + }, + "source": [ + "**1. Write a function that takes file name as an input and returns the content of that file as an output. You will call this function for a file called idontexist.txt. This file doesn't exist, see what happens when you try to open this file. Now in your function handle file note opening exception and return a message to user saying that this file doesn't exist!**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "KU3Y60iFdOv4" + }, + "source": [ + "\n", + "```\n", + "def get_file_content(file_name):\n", + " # your code goes here\n", + "\n", + "get_file_content(\"idontexist.txt\")\n", + "```\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "-wgcd9fddQ15" + }, + "source": [ + "**2. Write a function to calculate area of a triangle. This function takes base and height as parameters and computes triangle area by using formula,**\n", + "\n", + "```\n", + "area = (base*height)/2\n", + "```\n", + "**Call this function using string arguments. See what happens. Now change your function such that it throws appropriate exception when data type of base and height is not a number. It should also throw exception when either base or height is negative**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "RGtRqReOdSjO" + }, + "source": [ + "\n", + "\n", + "```\n", + "def triangle_area(base,height):\n", + " # your code goes here\n", + "\n", + "triangle_area(20,10) # This works ok\n", + "triangle_area(\"abc\",10) # This should throw exception saying base value is invalid\n", + "triangle_area(23,\"\") # This should throw exception saying value of height is invalid\n", + "triangle_area(-5,10) # This should throw exception saying base value can't be negative\n", + "triangle_area(8,-3) # Throws exception saying height can't be negative\n", + "```" + ] + } + ], + "metadata": { + "colab": { + "name": "exception_handling.ipynb", + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.3" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +}